java - 是否可以标记 springboot 测试,以便它们仅在某些配置文件处于 Activity 状态时运行

标签 java testing spring-boot

我有两个配置文件:dev 和 default。当 Activity 配置文件为默认时,我想跳过一些(不是全部)测试。 是否有可能以某种方式标记这些测试?或者如何实现? 我用的是 Spring Boot 。这是我的父测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyServiceStarter.class, webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT,
        properties = {"flyway.locations=filesystem:../database/h2", "server.port=9100", "spring.profiles.default=dev"})
@Category(IntegrationTest.class)
public abstract class AbstractModuleIntegrationTest { ... }

最佳答案

我的同事找到了一个解决方案: 因此,如果您需要注释单独的测试,您可以使用 @IfProfileValue 注释:

@IfProfileValue(name ="spring.profiles.active", value ="default")
    @Test
    public void testSomething() {
        //testing logic
    }

此测试仅在默认配置文件处于 Activity 状态时运行

更新: 对于 Junit 5 使用:

@EnabledIfSystemProperty(named = "spring.profiles.active", matches = "default")

更多信息:https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#integration-testing-annotations-meta

关于java - 是否可以标记 springboot 测试,以便它们仅在某些配置文件处于 Activity 状态时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43851621/

相关文章:

testing - 是否有一个命令可以从 clojure.test 和 Speclj 运行测试?

testing - 模拟实体管理器

java - 无法使用 Spring Boot 应用程序映射 html 文件

java - REST 将 ManyToMany 发送回服务器

java - 使用对象的字段过滤 ArrayList

java - 基于键排序 TreeMap ,其中键是可变的

testing - 复制较差的 Wi-Fi 进行测试

java - 对象类型的 JAXB 属性抛出空指针异常?

java - Spring Boot PostMapping 反序列化长数组

java - where 子句中参数的动态数量