java JUnit @Before 与 @Rule

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43193188/
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-11-03 07:10:24  来源:igfitidea点击:

JUnit @Before vs @Rule

javajunitjunit4

提问by kgf3JfUtW

I understand that,

我明白那个,

  • @Beforeand @BeforeClassrun before each test, or the entire test class, respectively
  • @Ruleand @ClassRulewraps each test, or the entire test class, respectively.
  • @Before并分别@BeforeClass在每个测试或整个测试类之前运行
  • @Rule并分别@ClassRule包装每个测试或整个测试类。

Let's say I need to initialize some data before each test method,

假设我需要在每个测试方法之前初始化一些数据,

How do I decide between using @Beforeand @Rule? Under what conditions is one preferred over another?The same question also goes for @BeforeClassvs.@ClassRule.

我如何决定使用@Before@Rule?在什么条件下一个人比​​另一个人更受欢迎?同样的问题也适用于@BeforeClassvs. @ClassRule

采纳答案by Quwin

In order to use @Rule, you require a class that implements TestRule(preferred) or MethodRule, as can be read here. Whereas @Beforeand @Afterrequire a new method to be written in every test case, @Ruledoes not because it is only an instantiation of already existing code.

为了使用@Rule,您需要一个实现TestRule(首选) or 的类MethodRule,可以在此处阅读。而@Before@After需要一种新的方法在每个测试用例来写,@Rule不会因为这只是一个已经存在的代码的实例。

So, if you would use @Beforeand @Afterfor setUp()and tearDown()that you'll be using in many test cases, it is actually a better idea to use @Rulebecause of code reuse. If you have a test case that requires a unique @Beforeand/or @After, then these annotations are preferable.

所以,如果你会使用@Before,并@AftersetUp()tearDown(),你会使用在很多测试案例,它实际上是一个更好的主意,利用@Rulecode reuse。如果您有一个需要唯一@Before和/或的测试用例@After,那么最好使用这些注释。

For a bit more elaborate answer with a couple examples, take a look here. Ajit explains it very well.

有关几个示例的更详细的答案,请查看此处。Ajit 很好地解释了这一点。

回答by kgf3JfUtW

Indeed, as @Quwin suggested, accoridng to JUnit 4.12 API doc,

事实上,正如@Quwin 所建议的,根据JUnit 4.12 API 文档

TestRulecan do everything that could be done previously with methods annotated with @Before, @After, @BeforeClass, or @AfterClass, but TestRules are (1) more powerful, and (2) more easily shared between projects and classes.

TestRule可以完成以前使用@Before, @After, @BeforeClass, 或 注释的方法可以完成的所有事情@AfterClass,但是TestRules (1) 更强大,并且(2) 更容易在项目和类之间共享。



Ways that TestRules are more powerful:

方法是TestRules为更强大

There are known implementing classes of the TestRule, which are some usefuls rules you can use out-of-the-box,

有一些已知的 实现类TestRule,这些是一些可以开箱即用的有用规则,

For examples of how this can be useful, see these provided TestRules, or write your own:

  • ErrorCollector: collect multiple errors in one test method
  • ExpectedException: make flexible assertions about thrown exceptions
  • ExternalResource: start and stop a server, for example
  • TemporaryFolder: create fresh files, and delete after test
  • TestName: remember the test name for use during the method
  • TestWatcher: add logic at events during method execution
  • Timeout: cause test to fail after a set time
  • Verifier: fail test if object state ends up incorrect

有关这如何有用的示例,请参阅这些提供的 TestRules,或编写您自己的:

  • ErrorCollector: 在一种测试方法中收集多个错误
  • ExpectedException: 对抛出的异常做出灵活的断言
  • ExternalResource:例如启动和停止服务器
  • TemporaryFolder: 创建新文件,测试后删除
  • TestName: 记住方法中使用的测试名称
  • TestWatcher: 在方法执行期间在事件中添加逻辑
  • Timeout: 导致测试在设定的时间后失败
  • Verifier: 如果对象状态最终不正确,则测试失败

Another benefit of rules, is that multiple rules can be used in a single test case. You may want to use RuleChainto specify the order in which the rules should be run.

规则的另一个好处是可以在单个测试用例中使用多个规则。您可能希望使用RuleChain来指定规则运行的顺序。