Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 and Spring Framework 4.3.x

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)?

like image 216
Juergen Zimmermann Avatar asked Jan 19 '17 17:01

Juergen Zimmermann


1 Answers

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

like image 168
Ralf Stuckert Avatar answered Sep 28 '22 00:09

Ralf Stuckert