如何在Spring Boot中基于​​配置文件运行/关闭选择性测试

发布于 2021-02-02 11:44:59

我有一个spring-boot正在编写IT测试的应用程序。

测试数据来自application-dev.properties我激活dev配置文件时

这是我要进行测试的内容:

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests {

    @Autowired
    Environment env;

    @Test
    public void contextLoads() {
        System.out.println(Arrays.toString((env.getActiveProfiles())));

    }

}

服务IT测试

public class ServiceITTest extends ApplicationTests {


     @value
     String username;

     @value
     String address;

     @Autowired
     MyService myService;


      @Test
      public void check_for_valid_username_address(){
            myService.validate(username,address);
      }
}

我希望上述测试仅在设置“ dev”,“ qa”的配置文件时运行。默认情况下,它不应运行。

在Spring Boot测试中能否获得良好的控制?

关注者
0
被浏览
79
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    您将要使用@IfProfileValue注释。不幸的是,它不能直接在活动配置文件上运行,而是可以读取属性,因此,如果您仅在要对其运行测试的配置文件中定义特定属性,则可以在该特定属性上使用该注释。

    http://docs.spring.io/spring/docs/current/spring-framework-
    reference/html/integration-testing.html#integration-testing-annotations-
    junit



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看