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
c85f19b7
Commit
c85f19b7
authored
Jan 20, 2020
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Add profiles directly to the application environment for tests"
This reverts commit
487b9cbf
. Fixes gh-19788
parent
11efe172
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
45 deletions
+8
-45
SpringBootContextLoader.java
...gframework/boot/test/context/SpringBootContextLoader.java
+7
-1
SpringBootContextLoaderTests.java
...ework/boot/test/context/SpringBootContextLoaderTests.java
+1
-44
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java
View file @
c85f19b7
...
@@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.bind.Binder;
...
@@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.bind.Binder;
import
org.springframework.boot.context.properties.source.ConfigurationPropertySource
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertySource
;
import
org.springframework.boot.context.properties.source.MapConfigurationPropertySource
;
import
org.springframework.boot.context.properties.source.MapConfigurationPropertySource
;
import
org.springframework.boot.test.mock.web.SpringBootMockServletContext
;
import
org.springframework.boot.test.mock.web.SpringBootMockServletContext
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext
;
import
org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext
;
import
org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer
;
import
org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContext
;
...
@@ -92,7 +93,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
...
@@ -92,7 +93,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application
.
getSources
().
addAll
(
Arrays
.
asList
(
configLocations
));
application
.
getSources
().
addAll
(
Arrays
.
asList
(
configLocations
));
ConfigurableEnvironment
environment
=
getEnvironment
();
ConfigurableEnvironment
environment
=
getEnvironment
();
if
(!
ObjectUtils
.
isEmpty
(
config
.
getActiveProfiles
()))
{
if
(!
ObjectUtils
.
isEmpty
(
config
.
getActiveProfiles
()))
{
environment
.
setActiveProfiles
(
config
.
getActiveProfiles
());
setActiveProfiles
(
environment
,
config
.
getActiveProfiles
());
}
}
ResourceLoader
resourceLoader
=
(
application
.
getResourceLoader
()
!=
null
)
?
application
.
getResourceLoader
()
ResourceLoader
resourceLoader
=
(
application
.
getResourceLoader
()
!=
null
)
?
application
.
getResourceLoader
()
:
new
DefaultResourceLoader
(
getClass
().
getClassLoader
());
:
new
DefaultResourceLoader
(
getClass
().
getClassLoader
());
...
@@ -138,6 +139,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
...
@@ -138,6 +139,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
return
new
StandardEnvironment
();
return
new
StandardEnvironment
();
}
}
private
void
setActiveProfiles
(
ConfigurableEnvironment
environment
,
String
[]
profiles
)
{
TestPropertyValues
.
of
(
"spring.profiles.active="
+
StringUtils
.
arrayToCommaDelimitedString
(
profiles
))
.
applyTo
(
environment
);
}
protected
String
[]
getInlinedProperties
(
MergedContextConfiguration
config
)
{
protected
String
[]
getInlinedProperties
(
MergedContextConfiguration
config
)
{
ArrayList
<
String
>
properties
=
new
ArrayList
<>();
ArrayList
<
String
>
properties
=
new
ArrayList
<>();
// JMX bean names will clash if the same bean is used in multiple contexts
// JMX bean names will clash if the same bean is used in multiple contexts
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java
View file @
c85f19b7
...
@@ -22,8 +22,6 @@ import org.junit.Ignore;
...
@@ -22,8 +22,6 @@ import org.junit.Ignore;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.Environment
;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.MergedContextConfiguration
;
import
org.springframework.test.context.MergedContextConfiguration
;
import
org.springframework.test.context.TestContext
;
import
org.springframework.test.context.TestContext
;
...
@@ -91,40 +89,13 @@ public class SpringBootContextLoaderTests {
...
@@ -91,40 +89,13 @@ public class SpringBootContextLoaderTests {
assertKey
(
config
,
"variables"
,
"foo=FOO\n bar=BAR"
);
assertKey
(
config
,
"variables"
,
"foo=FOO\n bar=BAR"
);
}
}
@Test
public
void
noActiveProfiles
()
{
Environment
environment
=
getApplicationEnvironment
(
SimpleConfig
.
class
);
assertThat
(
environment
.
getActiveProfiles
()).
isEmpty
();
}
@Test
public
void
multipleActiveProfiles
()
{
Environment
environment
=
getApplicationEnvironment
(
MultipleActiveProfiles
.
class
);
assertThat
(
environment
.
getActiveProfiles
()).
containsExactly
(
"profile1"
,
"profile2"
);
}
@Test
public
void
activeProfileWithComma
()
{
Environment
environment
=
getApplicationEnvironment
(
ActiveProfileWithComma
.
class
);
assertThat
(
environment
.
getActiveProfiles
()).
containsExactly
(
"profile1,2"
);
}
private
Map
<
String
,
Object
>
getEnvironmentProperties
(
Class
<?>
testClass
)
{
private
Map
<
String
,
Object
>
getEnvironmentProperties
(
Class
<?>
testClass
)
{
TestContext
context
=
getTestContext
(
testClass
);
TestContext
context
=
new
ExposedTestContextManager
(
testClass
).
getExposedTestContext
(
);
MergedContextConfiguration
config
=
(
MergedContextConfiguration
)
ReflectionTestUtils
.
getField
(
context
,
MergedContextConfiguration
config
=
(
MergedContextConfiguration
)
ReflectionTestUtils
.
getField
(
context
,
"mergedContextConfiguration"
);
"mergedContextConfiguration"
);
return
TestPropertySourceUtils
.
convertInlinedPropertiesToMap
(
config
.
getPropertySourceProperties
());
return
TestPropertySourceUtils
.
convertInlinedPropertiesToMap
(
config
.
getPropertySourceProperties
());
}
}
private
Environment
getApplicationEnvironment
(
Class
<?>
testClass
)
{
TestContext
context
=
getTestContext
(
testClass
);
return
context
.
getApplicationContext
().
getEnvironment
();
}
private
TestContext
getTestContext
(
Class
<?>
testClass
)
{
return
new
ExposedTestContextManager
(
testClass
).
getExposedTestContext
();
}
private
void
assertKey
(
Map
<
String
,
Object
>
actual
,
String
key
,
Object
value
)
{
private
void
assertKey
(
Map
<
String
,
Object
>
actual
,
String
key
,
Object
value
)
{
assertThat
(
actual
.
containsKey
(
key
)).
as
(
"Key '"
+
key
+
"' not found"
).
isTrue
();
assertThat
(
actual
.
containsKey
(
key
)).
as
(
"Key '"
+
key
+
"' not found"
).
isTrue
();
assertThat
(
actual
.
get
(
key
)).
isEqualTo
(
value
);
assertThat
(
actual
.
get
(
key
)).
isEqualTo
(
value
);
...
@@ -172,20 +143,6 @@ public class SpringBootContextLoaderTests {
...
@@ -172,20 +143,6 @@ public class SpringBootContextLoaderTests {
}
}
@SpringBootTest
@ActiveProfiles
({
"profile1"
,
"profile2"
})
@ContextConfiguration
(
classes
=
Config
.
class
)
static
class
MultipleActiveProfiles
{
}
@SpringBootTest
@ActiveProfiles
({
"profile1,2"
})
@ContextConfiguration
(
classes
=
Config
.
class
)
static
class
ActiveProfileWithComma
{
}
@Configuration
@Configuration
static
class
Config
{
static
class
Config
{
...
...
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