Spring Loads all available property files regardless of my @TestPropertySource annotation

I have a Spring Boot 1.4.3 project. In the folder test/resourcesI have two properties files, say, a-test.propertiesand b-test.properties.

The validation 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 the properties are being loaded from b-test.properties(I checked this with 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 a-test.properties:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=email@gmail.com
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
spring.mail.username=myemail@gmail.com
spring.mail.password=myPassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
+4
source share
4 answers

I found the source of the problem.

I had a class in the test folder, 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);
    }
}

, , Spring Boot DefaultEmailServiceContextBasedTest

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

, , @PropertySource. , @TestPropertySource, .

0

SpringBootTest spring,

, , spring . spring TestContext Framework:

  • SpringBootContextLoader ContextLoader , @ContextConfiguration (loader =...).
  • @SpringBootConfiguration, @Configuration , .
  • , .
  • webEnvironment, , .
  • TestRestTemplate bean -, .

, , , @TestPropertySource(locations = "classpath:a-test.properties", inheritProperties=false) a-test.properties.

:

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

@TestPropertySource , properties,

, @TestPropertySource(locations = "classpath:b-test.properties", properties = {"spring.mail.host=smtp.gmail.com", "spring.mail.port=587", "spring.mail.username=email@gmail.com" ......})

+3

.

@SpringBootApplication @PropertySource.

, @PropertySource @TestPropertySource .

.

@SpringBootApplication @PropertySource.

, , .

, a-test.properties - @PropertySource , b-test.properties - @TestPropertySource .

@TestPropertySource b-test.properties, , .

Spring . Spring Boots @* , .

, @SpringBootApplication @SpringBootConfiguration . .

, @SpringBootApplication webEnvironment .

, .

http://docs.spring.io/spring-boot/docs/1.4.0.M3/reference/htmlsingle/#boot-features-testing-spring-boot-applications-detecting-config

@SpringBootTest(classes=MyTestApplication.class), .

More details here in java docs.

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

0
source

The easiest way to do what you need is to set up such a profile:

@ActiveProfiles ("test")

Then just write the application-test.properties file with the required properties to run the test.

0
source

Source: https://habr.com/ru/post/1666707/


All Articles