小编典典

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

spring-boot

我有一个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测试中能否获得良好的控制?


阅读 232

收藏
2020-05-30

共1个答案

小编典典

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

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

2020-05-30