Spring Boot 2.1.0 has JUnit5 dependencies, but how to get rid of it?

Viewed 2620

I've just upgraded my projects to use Spring Boot 2.1.0 (before it was 2.0.x) and i have compilation WARNINGS:

[WARNING] Cannot find annotation method 'value()' in type 'org.junit.jupiter.api.extension.ExtendWith': class file for org.junit.jupiter.api.extension.ExtendWith not found

I can add dependency org.junit.jupiter / junit-jupiter-api to solve the warning, but I feel it's a 'hack'.

I don't want to see that warning (especially that my projects treat warnings like errors) and I don't want to pollute my projects with unnecessary dependencies.

i'm using Maven, but i can see someone had the same problem with Gradle https://www.reddit.com/r/java/comments/9sogxf/spring_boot_210_released_now_with_java_11_support/

2 Answers

If you add the org.junit.jupiter:junit-jupiter-api dependency to your project the warning will go away. It shouldn't hurt, because it's only API jar and only in test scope.

If you are using the @SpringBootTest to set classes=:

@SpringBootTest(classes = {
        MyAutoConfiguration.class,
        MyAutoConfigurationIntegrationTest.TestContextConfiguration.class
    })

the answer is alluded to in its api documentation:

Annotation that can be specified on a test class that runs Spring Boot based tests. Provides the following features over and above the regular Spring TestContext Framework:

Uses SpringBootContextLoader as the default ContextLoader when no specific @ContextConfiguration(loader=...) is defined.

Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.

You can use @ContextConfiguration which does not depend on Junit5's @ExtendsWith:

@RunWith(SpringRunner.class)
@ContextConfiguration(loader = SpringBootContextLoader.class,
    classes = {
        MyAutoConfiguration.class,
        MyAutoConfigurationIntegrationTest.TestContextConfiguration.class
    })
Related