JUnit 5 and Spring Framework 4.3.x

自古美人都是妖i 提交于 2019-12-10 13:24:09

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/41747849/junit-5-and-spring-framework-4-3-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!