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
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:
a-test.properties
b-test.properties
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]" ......})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With