@IfProfileValue not working with JUnit 5 SpringExtension

大城市里の小女人 提交于 2021-02-08 13:19:38

问题


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() {
    }
}

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


回答1:


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

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

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 {}

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() {
}


来源:https://stackoverflow.com/questions/53718071/ifprofilevalue-not-working-with-junit-5-springextension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!