@IfProfileValue 不适用于 JUnit 5 SpringExtension

@IfProfileValue not working with JUnit 5 SpringExtension

我将 junit5 与 spring-starter-test 一起使用,为了 运行 spring 测试我需要使用 @ExtendWith 而不是 @RunWith。然而 @IfProfileValue@RunWith(SpringRunner.class) 一起工作但不与 @ExtendWith(SpringExtension.class) 一起工作,下面是我的代码:

@SpringBootTest
@ExtendWith({SpringExtension.class})
class MyApplicationTests{

    @Test
    @DisplayName("Application Context should be loaded")
    @IfProfileValue(name = "test-groups" , value="unit-test")
    void contextLoads() {
    }
}

因此应该忽略 contextLoads,因为它没有指定 env 测试组。但是测试只是 运行 而忽略了 @IfProfileValue.

我发现@IfProfileValue只支持junit4,在junit5中我们将使用@EnabledIf@DisabledIf

引用https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#integration-testing-annotations-meta

更新:感谢@SamBrannen 的评论,所以我使用 junit5 内置支持与正则表达式匹配并将其作为注释。

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@EnabledIfSystemProperty(named = "test-groups", matches = "(.*)unit-test(.*)")
public @interface EnableOnIntegrationTest {}

所以任何测试方法或 class 只有当它们有一个包含单元测试的测试组系统 属性 时才会 运行。

@Test
@DisplayName("Application Context should be loaded")
@EnableOnIntegrationTest
void contextLoads() {
}