【问题标题】:how to load a different application.properties file in SpringBootTest如何在 SpringBootTest 中加载不同的 application.properties 文件
【发布时间】:2020-05-12 03:41:13
【问题描述】:

我在我的 Spring Boot 测试文件中使用了以下注释

@RunWith(SpringRunner.class) // tells JUnit to run using Spring’s testing support.
@SpringBootTest // is saying “bootstrap with Spring Boot’s support” (e.g. load application.properties and give me all the Spring Boot goodness)
@AutoConfigureMockMvc

当我测试休息控制器时,它会显示java.lang.IllegalStateException: Failed to load ApplicationContext

我检查了日志,这是由 JMS 和数据库的连接问题引起的。对于我的休息控制器测试,我不需要它们中的任何一个。但是它们的配置是在 application.properties 文件中定义的,并且会在 SpringBootTest 中加载,如何使用不同的 application.properties 文件进行测试?

【问题讨论】:

标签: java spring-boot junit spring-boot-test


【解决方案1】:

我们可以使用@TestPropertySource 或@PropertySource 来加载属性文件。

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:properties.yml")
@ActiveProfiles("test")
public class CartLoadApplicationTests {
    @Test
    public void contextLoads() {
    }    
}

【讨论】:

    【解决方案2】:

    这在我将 application.properties 文件放入测试目录后有效

    src/test/resources/application.properties
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-20
      • 2019-04-17
      • 2017-01-26
      • 2014-12-17
      • 2021-12-10
      • 2020-09-07
      • 1970-01-01
      • 2019-12-12
      相关资源
      最近更新 更多