Reputation: 13666
I have a Spring Boot 1.4.3 project.
In the test/resources
folder I have two properties file, let say
a-test.properties
and b-test.properties
.
The test class is annotated as follows:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:a-test.properties")
However, I see in my test that also the properties from b-test.properties
are loaded (I verified this via a simple print output).
Why? How can I prevent this?
Example extracted from my test
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:base-test.properties", inheritProperties=false)
public class EmailServiceContextBasedTest {
@SpyBean
public JavaMailSender javaMailSender;
@Before
public void setUp() throws Exception {
System.out.println(
((JavaMailSenderImpl)javaMailSender).getPassword()
);
System.out.println(
((JavaMailSenderImpl)javaMailSender).getJavaMailProperties()
);
}
@Test
public void test() throws Exception {
// do nothing
}
}
where the a-test.properties
is:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=false
and b-test.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=myPassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Upvotes: 6
Views: 2818
Reputation: 157
Easiest to do what you need is to set profile such:
@ActiveProfiles("test")
Then simply write an application-test.properties file with the desired properties for run your test.
Upvotes: 0
Reputation: 13666
I found the source of the issue.
I had in the test folder a class defined as in the following
@SpringBootApplication
@EnableAutoConfiguration
@PropertySource("classpath:a-test.properties")
public class TestApplication {
public static void main(final String... args) {
SpringApplication.run(TestApplication.class, args);
}
}
That was not called in the test, but I guess that was instantiated by default by Spring Boot when executing the tests in class DefaultEmailServiceContextBasedTest
with the annotation
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
So, the issue was that I used @PropertySource
. I don't know why, but replacing that annotation with @TestPropertySource
solved the conflict issue.
Upvotes: 0
Reputation: 4476
With SpringBootTest
annotation it will automaticaly the spring boot application configuration regarding to the docs
Annotation that can be specified on a test class that runs Spring Boot based tests. Provides the following features over and above the regular Spring TestContext Framework:
- Uses SpringBootContextLoader as the default ContextLoader when no specific @ContextConfiguration(loader=...) is defined.
- Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.
- Allows custom Environment properties to be defined using the properties attribute.
- Provides support for different webEnvironment modes, including the ability to start a fully running container listening on a defined or random port.
- Registers a TestRestTemplate bean for use in web tests that are using a fully running container.
So I suppose you load the other properties in other place, but the truth is @TestPropertySource(locations = "classpath:a-test.properties", inheritProperties=false)
will only load the a-test.properties
indeed.
Here is a simple test:
And with @TestPropertySource
annotation, you still get the change to override the properties before the tests run with the properties
attributes,
For you problem you can overrride it like @TestPropertySource(locations = "classpath:b-test.properties", properties = {"spring.mail.host=smtp.gmail.com", "spring.mail.port=587", "[email protected]" ......})
Upvotes: 5