Junit5 与 spring-boot 1.5 [英] Junit5 with spring-boot 1.5
问题描述
我有一个使用 spring-boot 版本 1.5.9.RELEASE
的 spring-boot 应用程序.为了测试这个应用程序,我想使用 junit-jupiter 版本 5.0.2
.
I have a spring-boot application which uses spring-boot version 1.5.9.RELEASE
. To test this application I want to use junit-jupiter version 5.0.2
.
对于简单的服务测试,它可以正常工作.但是在测试休息端点时,我失败了.原因是 @RunWith(SpringRunner.class)
注释,我用它和 junit4 将所有东西连接在一起.
For simple service tests it works without any problems. But when it comes to testing rest endpoints, I am failing. The reason is the @RunWith(SpringRunner.class)
annotation, which I used with junit4 to wire the everything together.
在 spring-boot 2 之前是否有 junit5 的 SpringRunner
?
Is there a SpringRunner
for junit5 before spring-boot 2?
更新
我偶然发现了一篇关于如何运行 JUnit5 测试的文章.第 4 点看起来是一个很有希望的开始.
I just stumbled over an article on how to run JUnit5 tests. Point 4 looks like a promising start.
推荐答案
要从 JUnit 4 迁移到 JUnit 5,您可以将 @RunWith(SpringRunner.class)
替换为 @ExtendWith(SpringExtension).class)
.
To migrate from JUnit 4 to JUnit 5 you can replace @RunWith(SpringRunner.class)
with @ExtendWith(SpringExtension.class)
.
遗憾的是,spring-boot 版本 1.5.9-RELEASE
基于 Spring 4,SpringExtension
仅在 Spring 5 之后可用.
Unfortunately, spring-boot version 1.5.9-RELEASE
is based on Spring 4 and the SpringExtension
is only available since Spring 5.
但是,有一个解决方案,因为可以通过使用 spring-test-junit5
在 Spring 4 中使用 JUnit 5 和 SpringExtension
.只需查看说明以设置依赖项.
However, there is a solution since it's possible to use JUnit 5 and SpringExtension
in Spring 4 by using spring-test-junit5
. Just check the instructions for setting up the dependencies.
这篇关于Junit5 与 spring-boot 1.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!