Chi*_*Dov 6 junit spring-test spring-profiles spring-boot junit5
我使用junit5和spring-starter-test,为了运行spring test我需要使用@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() {
}
}
Run Code Online (Sandbox Code Playgroud)
所以contextLoads应该被忽略,因为它没有指定env test-grooups.但测试只是运行而忽略了@IfProfileValue
.
我发现@IfProfileValue
只支持junit4,在junit5中我们将使用@EnabledIf
和@DisabledIf
.
更新:感谢@ SamBrannen的评论,所以我使用junit5内置支持与正则表达式匹配,并将其作为注释.
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@EnabledIfSystemProperty(named = "test-groups", matches = "(.*)unit-test(.*)")
public @interface EnableOnIntegrationTest {}
Run Code Online (Sandbox Code Playgroud)
所以任何具有此注释的测试方法或类只有在具有包含单元测试的测试组的系统属性时才会运行.
@Test
@DisplayName("Application Context should be loaded")
@EnableOnIntegrationTest
void contextLoads() {
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
759 次 |
最近记录: |