无论我的@TestPropertySource注释如何,Spring Boot都会加载所有可用的属性文件

mat*_*boy 6 spring unit-testing properties spring-boot

我有一个Spring Boot 1.4.3项目.在test/resources文件夹中我有两个属性文件,让我们说 a-test.propertiesb-test.properties.

测试类注释如下:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:a-test.properties")
Run Code Online (Sandbox Code Playgroud)

但是,我在测试中看到也b-test.properties加载了属性(我通过简单的打印输出验证了这一点).

为什么?我怎么能阻止这个?


从我的测试中提取的示例

@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
    }

}
Run Code Online (Sandbox Code Playgroud)

在哪里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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Lip*_*ang 5

带有SpringBootTest注释,它将自动对文档进行spring boot应用程序配置

可以在运行基于Spring Boot的测试的测试类上指定的注释。除了常规的Spring TestContext Framework之外,还提供以下功能:

  • 当未定义特定的@ContextConfiguration(loader = ...)时,将SpringBootContextLoader用作默认的ContextLoader。
  • 不使用嵌套@Configuration且未指定显式类时,自动搜索@SpringBootConfiguration。
  • 允许使用properties属性定义自定义环境属性。
  • 提供对不同的webEnvironment模式的支持,包括启动在定义的或随机端口上侦听的完全运行的容器的功能。
  • 注册一个TestRestTemplate bean,以在使用完全运行的容器的Web测试中使用。

所以我想您在其他地方加载了其他属性,但事实是@TestPropertySource(locations = "classpath:a-test.properties", inheritProperties=false)只会加载a-test.properties实际的。

这是一个简单的测试:

  • a-test.properties 在此处输入图片说明
  • b-test.properties 在此处输入图片说明

借助@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" ......})


mat*_*boy 0

我找到了问题的根源。

我在测试文件夹中有一个定义如下的类

@SpringBootApplication
@EnableAutoConfiguration
@PropertySource("classpath:a-test.properties")
public class TestApplication {

    public static void main(final String... args) {
        SpringApplication.run(TestApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

测试中没有调用它,但我猜想在DefaultEmailServiceContextBasedTest使用注释在类中执行测试时,Spring Boot 默认实例化了它

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Run Code Online (Sandbox Code Playgroud)

所以,问题是我使用了@PropertySource. 我不知道为什么,但替换该注释@TestPropertySource解决了冲突问题。