Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot loads all the available properties file regardless of my @TestPropertySource annotation

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
like image 252
mat_boy Avatar asked Jan 13 '17 10:01

mat_boy


1 Answers

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:

  • with a-test.properties enter image description here
  • with b-test.properties enter image description here

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]" ......})

like image 110
Liping Huang Avatar answered Sep 20 '22 15:09

Liping Huang