Java 如何在 Junit 5 中替换 @Rule 注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51012335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:54:58  来源:igfitidea点击:

How to replace @Rule annotation in Junit 5?

javajunit5junit-rule

提问by IKo

I'm using wiremock in my tests and have such a line of code:

我在我的测试中使用了 wiremock 并且有这样一行代码:

@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);

I want to switch to Junit 5. So I added the next dependency (using gradle):

我想切换到 Junit 5。所以我添加了下一个依赖项(使用 gradle):

testCompile('org.junit.jupiter:junit-jupiter-engine:5.1.1')

But there no suggestions when I'm trying to import @Rule annotation. Do I need to add another module of junit dependency? Or rules are not supported in Junit5? If not how can I replace @Rule annotation to make tests work again? Thanks.

但是当我尝试导入 @Rule 注释时没有任何建议。我需要添加另一个junit依赖模块吗?还是 Junit5 不支持规则?如果不是,我该如何替换 @Rule 注释以使测试再次工作?谢谢。

采纳答案by davidxxx

In a general way, what you did with @Ruleand @ClassRulein JUnit 4 should be done with @ExtendWithand Extensionthat associated provide a very close feature in JUnit 5.
It works as standards JUnit lifecycle hooks but that it is extracted in a Extensionclass. And similarly to @Rule, as many Extensions as required may be added for a test class.

一般而言,您在 JUnit 4 中所做的@Rule@ClassRule在 JUnit 4 中所做的都应该完成,@ExtendWith并且Extension相关联在 JUnit 5 中提供了一个非常接近的特性。
它作为标准的 JUnit 生命周期钩子工作,但它是在一个Extension类中提取的。与 类似@RuleExtension可以为测试类添加所需数量的 s。

To handle the issue you have several possible approaches among :

要处理这个问题,您有几种可能的方法:

  • keep the JUnit 4 way (JUnit 5 owns the JUnit Vintage part that allows to execute JUnit 3 or 4 tests).
  • rewrite the @Ruleas an Extension.
  • do the actual processing done by WireMockRule(start the server, execute your tests and stop the server) in each test of class with @BeforeEachand @AfterEachhook methods.
  • use a third library that implements the equivalent of WireMockRule in the JUnit 5 Extension way such as https://github.com/lanwen/wiremock-junit5
  • 保持 JUnit 4 的方式(JUnit 5 拥有允许执行 JUnit 3 或 4 测试的 JUnit Vintage 部分)。
  • 将 重写@RuleExtension.
  • WireMockRule在类的每个测试中执行由(启动服务器,执行测试并停止服务器)完成的实际处理@BeforeEach@AfterEach钩子方法。
  • 使用在 JUnit 5 扩展方式中实现等效于 WireMockRule 的第三个库,例如https://github.com/lanwen/wiremock-junit5

Note that your issue already discussed in the JUnit 5 Issues.

请注意,您的问题已在JUnit 5 问题中讨论过。

回答by Arho Huttunen

JUnit 4 annotations @Ruleand @ClassRuledo not exist in JUnit 5. Basically there is a new extension model that can be used to implement extensions with the same functionality. These extensions can be used with the @ExtendWithannotation.

JUnit 4 注释@Rule并且@ClassRule在 JUnit 5 中不存在。基本上有一个新的扩展模型,可用于实现具有相同功能的扩展。这些扩展可以与@ExtendWith注释一起使用。

There is a limited migration support for a subset of JUnit 4 rules in the junit-jupiter-migrationsupportmodule. Unfortunately, it's only limited to subclasses of ExternalResourceand Verifier.

junit-jupiter-migrationsupport模块中对 JUnit 4 规则子集的迁移支持有限。不幸的是,它仅限于ExternalResource和的子类Verifier

Before wiremock has official support for JUnit you have some workarounds:

在wiremock 正式支持JUnit 之前,您有一些解决方法:

  1. Run JUnit 4 tests side by side with JUnit 5 tests with the junit-vintage-engine.
  2. Start and stop the server yourself in the test code.
  3. Use a 3rd party extension like wiremock-junit5or wiremock-extension.
  1. 使用junit-vintage-engine并排运行 JUnit 4 测试和 JUnit 5 测试。
  2. 在测试代​​码中自己启动和停止服务器。
  3. 使用像wiremock-junit5wiremock-extension这样的 3rd 方扩展