JUnit 5 and Spring Framework 4.3.x

Juergen Zimmermann picture Juergen Zimmermann · Jan 19, 2017 · Viewed 9.6k times · Source

Is it right, that JUnit 4.12 and junit-vintage-engine (from JUnit 5) can be used together with Spring Framework 4.3.x? Is there a possibility to also use junit-jupiter-api and junit-jupiter-engine (both from JUnit 5)?

Answer

Ralf Stuckert picture Ralf Stuckert · Apr 10, 2017

I assume you mean Spring integration tests, something like:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ExampleIT {
    @Test
    public void test() {
    }
}

This can be run with the vintage engine.

You can also use JUnit 5 with Spring 4.3 using the prototype for Spring 5. The Jars are available on Jitpack, so in order to convert this test to JUnit 5 just add the Jupiter API and the prototype to your dependencies, e.g. for maven

<dependency>
   <groupId>com.github.sbrannen</groupId>
   <artifactId>spring-test-junit5</artifactId>
   <version>1.0.0.M3</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.junit.jupiter</groupId> 
   <artifactId>junit-jupiter-api</artifactId>
   <version>5.0.0-M3</version>
   <scope>test</scope>
</dependency>
...
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

And instead of the SpringRunner use the SpringExtension and the JUnit jupiter API:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class ExampleIT {
    @Test
    public void test() {
    }
}

Hope this helps