Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/

like image 850
razor Avatar asked Nov 15 '18 12:11

razor


People also ask

Does spring-boot-starter-test include JUnit5?

Important note. Spring Boot 2.3. 0 includes junit5 as the default library for unit testing.

Is JUnit removed from spring boot?

After few research I got to know that Spring-boot 2.4 onwards, JUnit4 has been removed.

Is JUnit included in spring boot?

If you use the spring-boot-starter-test 'Starter' (in the test scope ), you will find the following provided libraries: JUnit — The de-facto standard for unit testing Java applications.

Should I use JUnit5?

JUnit 5 leverages features from Java 8 or later, like lambda functions, making tests more powerful and easier to maintain. JUnit 5 has added some very useful new features for describing, organizing, and executing tests. For instance, tests get better display names and can be organized hierarchically.


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.

like image 76
DAN Avatar answered Sep 29 '22 19:09

DAN


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
    })
like image 42
Alun Avatar answered Sep 29 '22 21:09

Alun