【问题标题】:Spring Boot loads all the available properties file regardless of my @TestPropertySource annotation无论我的 @TestPropertySource 注释如何,Spring Boot 都会加载所有可用的属性文件
【发布时间】:2017-05-28 16:11:18
【问题描述】:

我有一个 Spring Boot 1.4.3 项目。 在test/resources 文件夹中,我有两个属性文件,比如说 a-test.properties 和 b-test.properties。

测试类注释如下:

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

但是,我在测试中看到 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
    }

}

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

和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

【问题讨论】:

  • 您是如何阅读这些属性的?您能否添加一些代码来展示您所描述的行为?
  • @dimitrisli 是的,当然!我附上了一个例子
  • @mat_boy 请提供你得到的输出和你期望的输出。您的示例只会打印与属性文件之一关联的密码(在我的例子中是a-test.properties)。那你是怎么验证that also the properties from b-test.properties are loaded的?!

标签: spring unit-testing spring-boot properties


【解决方案1】:

使用 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" ......}) 一样覆盖它

【讨论】:

    【解决方案2】:

    我找到了问题的根源。

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

    @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 替换该注释解决了冲突问题。

    【讨论】:

      【解决方案3】:

      您需要做的最简单的事情是设置如下配置文件:

      @ActiveProfiles("测试")

      然后只需编写一个 application-test.properties 文件,其中包含运行测试所需的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-24
        • 2019-03-20
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 2014-11-26
        相关资源
        最近更新 更多