我在我的测试中使用 WireMock 并且有这样一行代码:
@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
我想切换到 JUnit 5。所以我添加了下一个依赖项(使用 Gradle):
testCompile('org.junit.jupiter:junit-jupiter-engine:5.1.1')
但是当我尝试导入 @Rule
注释时没有任何建议。
我需要添加另一个 JUnit 依赖模块吗?还是 JUnit 5 不支持规则?如果没有,我如何替换 @Rule
注释以使测试再次工作?
原文由 IKo 发布,翻译遵循 CC BY-SA 4.0 许可协议
In a general way, what you did with
@Rule
and@ClassRule
in JUnit 4 should be done with@ExtendWith
andExtension
that associated provide a JUnit 5 中非常接近的功能。它作为标准 JUnit 生命周期挂钩工作,但它是在
Extension
类中提取的。与@Rule
类似,可以为测试类添加所需的Extension
s。要处理此问题,您有几种可能的方法:
@Rule
重写为Extension
。WireMockRule
(启动服务器,执行测试并停止服务器)进行实际处理@BeforeEach
和@AfterEach
hook 方法请注意,您的问题已在 JUnit 5 Issues 中讨论过。