JUnit 5和Spring Framework 4.3.x [英] JUnit 5 and Spring Framework 4.3.x
问题描述
可以将JUnit 4.12和junit-vintage-engine(来自JUnit 5)与Spring Framework 4.3.x一起使用吗? 是否有可能同时使用junit-jupiter-api和junit-jupiter-engine(均来自JUnit 5)?
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)?
推荐答案
我假设您的意思是Spring集成测试,例如:
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.
您还可以使用 Spring 5的原型将JUnit 5与Spring 4.3结合使用. Jars在Jitpack上可用,因此为了将测试转换为JUnit 5,只需将Jupiter API和原型添加到依赖项中即可,例如对于Maven
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>
使用SpringExtension和JUnit jupiter API代替SpringRunner:
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() {
}
}
希望这会有所帮助
这篇关于JUnit 5和Spring Framework 4.3.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!