14

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.

Chi Dov
  • 1,447
  • 1
  • 11
  • 22

1 Answers1

16

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() {
}
Chi Dov
  • 1,447
  • 1
  • 11
  • 22
  • 4
    That's correct. However, if you're using JUnit Jupiter 5.1 or higher and you want to check a JVM system property, the best choice is the built-in support for `@EnabledIfSystemProperty` and `@DisabledIfSystemProperty`. – Sam Brannen Dec 11 '18 at 16:27