如何在Spring Boot中基于​​配置文件运行/关闭选择性测试 [英] how to run/turn off selective tests based on profiles in spring boot

查看:101
本文介绍了如何在Spring Boot中基于​​配置文件运行/关闭选择性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我要进行的测试:

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

    @Autowired
    Environment env;

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

    }

}

ServiceITTest

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"时运行上述测试.默认情况下,它不应运行.

在春季启动测试中能否获得良好的控制?

解决方案

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

Is it possible to get that fine control in spring boot testing?

You would want to use the @IfProfileValue annotation. Unfortunately it doesn't work directly on the active profiles but it can read a property so if you only define a specific property within the profiles that you want to run the test on then you can use that annotation on that specific property.

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

这篇关于如何在Spring Boot中基于​​配置文件运行/关闭选择性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆