【问题标题】:SpringBoot jUnit is not loading application-dev.xmlSpringBoot jUnit 未加载 application-dev.xml
【发布时间】:2017-04-18 07:18:27
【问题描述】:

我正在使用 SpringBoot 进行最新开发。我们有基于配置文件的配置,所以那里有 3 个文件

application-{env}.properties,env表示dev、stage和prod

当我在 Eclipse 中使用 -Dspring.profiles.active=dev 运行配置在本地运行项目时,它会从 application-dev.properties 中选择配置。但是如果我尝试运行一个 jUnit,它期望 application.properties 否则会抛出错误。我把一个 application.properties 它工作正常。请告诉我如何使用基于配置文件的 JUnit 来选择 application-dev.properties .请在此提供您的意见,在此先感谢。

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProcessingServiceTest {
    @Test
    public void testlet2MeIn() throws Exception{
}
}

【问题讨论】:

    标签: java eclipse spring-boot junit


    【解决方案1】:

    你试过setProperty吗?

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProcessingServiceTest {
        public ProcessingServiceTest(){
           System.setProperty("spring.profiles.active","dev");
        }
        @Test
        public void testlet2MeIn() throws Exception{
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用@TestPropertySource指定属性文件并指定应该使用哪个文件来加载测试属性

      @TestPropertySource(locations = "classpath:application-dev.properties")
      

      或者 最简单的方法是在你的测试类上使用它@ActiveProfiles("dev")

      【讨论】:

      • 您可以在测试类上使用此注释,而不是按照其他答案中的建议明确设置配置文件。 @ActiveProfiles("dev") 或 @TestPropertySource(locations = "classpath:application-dev.properties")
      猜你喜欢
      • 2019-08-25
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多