我将 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 {}
所以任何带有此注释的测试方法或类只有在它们具有包含单元测试的 test-groups 系统属性时才会运行。
@Test
@DisplayName("Application Context should be loaded")
@EnableOnIntegrationTest
void contextLoads() {
}
关于spring-boot - @IfProfileValue 不适用于 JUnit 5 SpringExtension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718071/