问题
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