Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
b3ccefdb
Commit
b3ccefdb
authored
Apr 23, 2020
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.2.x'
Closes gh-21102
parents
ddcd1bc7
4dc9bbe1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
9 deletions
+120
-9
SpringBootContextLoader.java
...gframework/boot/test/context/SpringBootContextLoader.java
+7
-4
SpringBootTestArgsTrackingContextCustomizer.java
.../context/SpringBootTestArgsTrackingContextCustomizer.java
+69
-0
SpringBootTestContextBootstrapper.java
.../boot/test/context/SpringBootTestContextBootstrapper.java
+6
-3
SpringBootTestContextBootstrapperTests.java
...ext/bootstrap/SpringBootTestContextBootstrapperTests.java
+38
-2
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java
View file @
b3ccefdb
...
@@ -78,8 +78,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
...
@@ -78,8 +78,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
*/
*/
public
class
SpringBootContextLoader
extends
AbstractContextLoader
{
public
class
SpringBootContextLoader
extends
AbstractContextLoader
{
private
static
final
String
[]
NO_ARGS
=
new
String
[
0
];
@Override
@Override
public
ApplicationContext
loadContext
(
MergedContextConfiguration
config
)
throws
Exception
{
public
ApplicationContext
loadContext
(
MergedContextConfiguration
config
)
throws
Exception
{
Class
<?>[]
configClasses
=
config
.
getClasses
();
Class
<?>[]
configClasses
=
config
.
getClasses
();
...
@@ -145,11 +143,16 @@ public class SpringBootContextLoader extends AbstractContextLoader {
...
@@ -145,11 +143,16 @@ public class SpringBootContextLoader extends AbstractContextLoader {
* empty array.
* empty array.
* @param config the source context configuration
* @param config the source context configuration
* @return the application arguments to use
* @return the application arguments to use
* @deprecated since 2.2.7
* @see SpringApplication#run(String...)
* @see SpringApplication#run(String...)
*/
*/
protected
String
[]
getArgs
(
MergedContextConfiguration
config
)
{
protected
String
[]
getArgs
(
MergedContextConfiguration
config
)
{
return
MergedAnnotations
.
from
(
config
.
getTestClass
(),
SearchStrategy
.
TYPE_HIERARCHY
).
get
(
SpringBootTest
.
class
)
ContextCustomizer
customizer
=
config
.
getContextCustomizers
().
stream
()
.
getValue
(
"args"
,
String
[].
class
).
orElse
(
NO_ARGS
);
.
filter
((
c
)
->
c
instanceof
SpringBootTestArgsTrackingContextCustomizer
).
findFirst
().
orElse
(
null
);
if
(
customizer
!=
null
)
{
return
((
SpringBootTestArgsTrackingContextCustomizer
)
customizer
).
getArgs
();
}
return
SpringBootTestArgsTrackingContextCustomizer
.
NO_ARGS
;
}
}
private
void
setActiveProfiles
(
ConfigurableEnvironment
environment
,
String
[]
profiles
)
{
private
void
setActiveProfiles
(
ConfigurableEnvironment
environment
,
String
[]
profiles
)
{
...
...
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgsTrackingContextCustomizer.java
0 → 100644
View file @
b3ccefdb
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
test
.
context
;
import
java.util.Arrays
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.core.annotation.MergedAnnotations
;
import
org.springframework.test.context.ContextCustomizer
;
import
org.springframework.test.context.MergedContextConfiguration
;
/**
* {@link ContextCustomizer} to track application arguments that are used in a
* {@link SpringBootTest}. The application arguments are taken into account when
* evaluating a {@link MergedContextConfiguration} to determine if a context can be shared
* between tests.
*
* @author Madhura Bhave
*/
class
SpringBootTestArgsTrackingContextCustomizer
implements
ContextCustomizer
{
static
final
String
[]
NO_ARGS
=
new
String
[
0
];
private
String
[]
args
;
SpringBootTestArgsTrackingContextCustomizer
(
Class
<?>
testClass
)
{
this
.
args
=
MergedAnnotations
.
from
(
testClass
,
MergedAnnotations
.
SearchStrategy
.
TYPE_HIERARCHY
)
.
get
(
SpringBootTest
.
class
).
getValue
(
"args"
,
String
[].
class
).
orElse
(
NO_ARGS
);
}
@Override
public
void
customizeContext
(
ConfigurableApplicationContext
context
,
MergedContextConfiguration
mergedConfig
)
{
}
/**
* Return the application arguments that are tracked by this customizer.
* @return the args
*/
String
[]
getArgs
()
{
return
this
.
args
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
return
(
obj
!=
null
)
&&
(
getClass
()
==
obj
.
getClass
())
&&
Arrays
.
equals
(
this
.
args
,
((
SpringBootTestArgsTrackingContextCustomizer
)
obj
).
args
);
}
@Override
public
int
hashCode
()
{
return
Arrays
.
hashCode
(
this
.
args
);
}
}
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java
View file @
b3ccefdb
...
@@ -19,6 +19,7 @@ package org.springframework.boot.test.context;
...
@@ -19,6 +19,7 @@ package org.springframework.boot.test.context;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -40,6 +41,7 @@ import org.springframework.core.env.Environment;
...
@@ -40,6 +41,7 @@ import org.springframework.core.env.Environment;
import
org.springframework.core.io.support.SpringFactoriesLoader
;
import
org.springframework.core.io.support.SpringFactoriesLoader
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfigurationAttributes
;
import
org.springframework.test.context.ContextConfigurationAttributes
;
import
org.springframework.test.context.ContextCustomizer
;
import
org.springframework.test.context.ContextHierarchy
;
import
org.springframework.test.context.ContextHierarchy
;
import
org.springframework.test.context.ContextLoader
;
import
org.springframework.test.context.ContextLoader
;
import
org.springframework.test.context.MergedContextConfiguration
;
import
org.springframework.test.context.MergedContextConfiguration
;
...
@@ -355,11 +357,12 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
...
@@ -355,11 +357,12 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
*/
*/
protected
final
MergedContextConfiguration
createModifiedConfig
(
MergedContextConfiguration
mergedConfig
,
protected
final
MergedContextConfiguration
createModifiedConfig
(
MergedContextConfiguration
mergedConfig
,
Class
<?>[]
classes
,
String
[]
propertySourceProperties
)
{
Class
<?>[]
classes
,
String
[]
propertySourceProperties
)
{
Set
<
ContextCustomizer
>
contextCustomizers
=
new
LinkedHashSet
<>(
mergedConfig
.
getContextCustomizers
());
contextCustomizers
.
add
(
new
SpringBootTestArgsTrackingContextCustomizer
(
mergedConfig
.
getTestClass
()));
return
new
MergedContextConfiguration
(
mergedConfig
.
getTestClass
(),
mergedConfig
.
getLocations
(),
classes
,
return
new
MergedContextConfiguration
(
mergedConfig
.
getTestClass
(),
mergedConfig
.
getLocations
(),
classes
,
mergedConfig
.
getContextInitializerClasses
(),
mergedConfig
.
getActiveProfiles
(),
mergedConfig
.
getContextInitializerClasses
(),
mergedConfig
.
getActiveProfiles
(),
mergedConfig
.
getPropertySourceLocations
(),
propertySourceProperties
,
mergedConfig
.
getPropertySourceLocations
(),
propertySourceProperties
,
contextCustomizers
,
mergedConfig
.
getContextCustomizers
(),
mergedConfig
.
getContextLoader
(),
mergedConfig
.
getContextLoader
(),
getCacheAwareContextLoaderDelegate
(),
mergedConfig
.
getParent
());
getCacheAwareContextLoaderDelegate
(),
mergedConfig
.
getParent
());
}
}
}
}
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/bootstrap/SpringBootTestContextBootstrapperTests.java
View file @
b3ccefdb
...
@@ -23,8 +23,11 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...
@@ -23,8 +23,11 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import
org.springframework.boot.test.context.SpringBootTestContextBootstrapper
;
import
org.springframework.boot.test.context.SpringBootTestContextBootstrapper
;
import
org.springframework.test.context.BootstrapContext
;
import
org.springframework.test.context.BootstrapContext
;
import
org.springframework.test.context.CacheAwareContextLoaderDelegate
;
import
org.springframework.test.context.CacheAwareContextLoaderDelegate
;
import
org.springframework.test.context.TestContext
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalStateException
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalStateException
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
@@ -50,15 +53,33 @@ class SpringBootTestContextBootstrapperTests {
...
@@ -50,15 +53,33 @@ class SpringBootTestContextBootstrapperTests {
buildTestContext
(
SpringBootTestMockWebEnvironmentAndWebAppConfiguration
.
class
);
buildTestContext
(
SpringBootTestMockWebEnvironmentAndWebAppConfiguration
.
class
);
}
}
@Test
void
mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual
()
{
TestContext
context
=
buildTestContext
(
SpringBootTestArgsConfiguration
.
class
);
Object
contextConfiguration
=
ReflectionTestUtils
.
getField
(
context
,
"mergedContextConfiguration"
);
TestContext
otherContext2
=
buildTestContext
(
SpringBootTestOtherArgsConfiguration
.
class
);
Object
otherContextConfiguration
=
ReflectionTestUtils
.
getField
(
otherContext2
,
"mergedContextConfiguration"
);
assertThat
(
contextConfiguration
).
isNotEqualTo
(
otherContextConfiguration
);
}
@Test
void
mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual
()
{
TestContext
context
=
buildTestContext
(
SpringBootTestArgsConfiguration
.
class
);
Object
contextConfiguration
=
ReflectionTestUtils
.
getField
(
context
,
"mergedContextConfiguration"
);
TestContext
otherContext2
=
buildTestContext
(
SpringBootTestSameArgsConfiguration
.
class
);
Object
otherContextConfiguration
=
ReflectionTestUtils
.
getField
(
otherContext2
,
"mergedContextConfiguration"
);
assertThat
(
contextConfiguration
).
isEqualTo
(
otherContextConfiguration
);
}
@SuppressWarnings
(
"rawtypes"
)
@SuppressWarnings
(
"rawtypes"
)
private
void
buildTestContext
(
Class
<?>
testClass
)
{
private
TestContext
buildTestContext
(
Class
<?>
testClass
)
{
SpringBootTestContextBootstrapper
bootstrapper
=
new
SpringBootTestContextBootstrapper
();
SpringBootTestContextBootstrapper
bootstrapper
=
new
SpringBootTestContextBootstrapper
();
BootstrapContext
bootstrapContext
=
mock
(
BootstrapContext
.
class
);
BootstrapContext
bootstrapContext
=
mock
(
BootstrapContext
.
class
);
bootstrapper
.
setBootstrapContext
(
bootstrapContext
);
bootstrapper
.
setBootstrapContext
(
bootstrapContext
);
given
((
Class
)
bootstrapContext
.
getTestClass
()).
willReturn
(
testClass
);
given
((
Class
)
bootstrapContext
.
getTestClass
()).
willReturn
(
testClass
);
CacheAwareContextLoaderDelegate
contextLoaderDelegate
=
mock
(
CacheAwareContextLoaderDelegate
.
class
);
CacheAwareContextLoaderDelegate
contextLoaderDelegate
=
mock
(
CacheAwareContextLoaderDelegate
.
class
);
given
(
bootstrapContext
.
getCacheAwareContextLoaderDelegate
()).
willReturn
(
contextLoaderDelegate
);
given
(
bootstrapContext
.
getCacheAwareContextLoaderDelegate
()).
willReturn
(
contextLoaderDelegate
);
bootstrapper
.
buildTestContext
();
return
bootstrapper
.
buildTestContext
();
}
}
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
...
@@ -73,4 +94,19 @@ class SpringBootTestContextBootstrapperTests {
...
@@ -73,4 +94,19 @@ class SpringBootTestContextBootstrapperTests {
}
}
@SpringBootTest
(
args
=
"--app.test=same"
)
static
class
SpringBootTestArgsConfiguration
{
}
@SpringBootTest
(
args
=
"--app.test=same"
)
static
class
SpringBootTestSameArgsConfiguration
{
}
@SpringBootTest
(
args
=
"--app.test=different"
)
static
class
SpringBootTestOtherArgsConfiguration
{
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment