Issue
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
.
Solution
I found out that @IfProfileValue
only support for junit4, in junit5 we will use @EnabledIf
and @DisabledIf
.
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() {
}
Answered By - Chi Dov
Answer Checked By - Gilberto Lyons (JavaFixing Admin)