@IfProfileValue无法与JUnit 5 SpringExtension一起使用 [英] @IfProfileValue not working with JUnit 5 SpringExtension

查看:109
本文介绍了@IfProfileValue无法与JUnit 5 SpringExtension一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use junit5 with spring-starter-test, in order to run spring test I need to use @ExtendWith instead of @RunWith. However @IfProfileValue work with @RunWith(SpringRunner.class) but not with @ExtendWith(SpringExtension.class), below is my code:

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

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

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

so the contextLoads should be ignore since it didn't specify the env test-grooups. but the test just run and ignore the @IfProfileValue.

推荐答案

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

I found out that @IfProfileValue only support for junit4, in junit5 we will use @EnabledIf and @DisabledIf.

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

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

Update: Thanks to @SamBrannen'scomment, so I use junit5 build-in support with regex matches and make it as an Annotation.

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

因此具有此注释的任何测试方法或类仅在其具有包含单元测试的test-groups的系统属性时才运行.

so any test method or class with this annotion will run only when they have a system property of test-groups which contains unit test.

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

这篇关于@IfProfileValue无法与JUnit 5 SpringExtension一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆