Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@IfProfileValue not working with JUnit 5 SpringExtension

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.

like image 679
Chi Dov Avatar asked Dec 11 '18 05:12

Chi Dov


1 Answers

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() {
}
like image 153
Chi Dov Avatar answered Nov 16 '22 19:11

Chi Dov