Java Junit @Rule 和 @ClassRule

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

Junit @Rule and @ClassRule

javajunitrule

提问by Viktar Kava

I am writing JUnit4 test in which I am using TemporaryFolder rule. It seems that it works fine with both @Rule and @ClassRule. What is the difference between Junit @Rule and @ClassRule? Why should I use one and not another?

我正在编写 JUnit4 测试,其中我使用了 TemporaryFolder 规则。似乎它适用于@Rule 和@ClassRule。Junit @Rule 和 @ClassRule 有什么区别?为什么我应该使用一个而不是另一个?

采纳答案by slim

The distinction becomes clear when you have more than one test method in a class.

当一个类中有多个测试方法时,区别就很明显了。

A @ClassRulehas its before()method run before any of the test methods. Then all the test methods are run, and finally the rule's after()method. So if you have five test methods in a class, before()and after()will still only get run once each.

A@ClassRulebefore()方法在任何测试方法之前运行。然后运行所有测试方法,最后运行规则的after()方法。因此,如果您在一个类中有五个测试方法,before()并且after()每个方法仍然只能运行一次。

@ClassRuleapplies to a static method, and so has all the limitations inherent in that.

@ClassRule适用于静态方法,因此具有所有固有的限制。

A @Rulecauses tests to be run via the rule's apply()method, which can do things before and after the target method is run. If you have five test methods, the rule's apply()is called five times, as a wrapper around each method.

A@Rule导致测试通过规则的apply()方法运行,它可以在目标方法运行之前和之后做一些事情。如果您有五个测试方法,则规则apply()被调用五次,作为每个方法的包装器。

Use @ClassRuleto set up something that can be reused by all the test methods, if you can achieve that in a static method.

使用@ClassRule设置的东西,可以通过所有的测试方法可以重复使用,如果你能做到这一点的静态方法。

Use @Ruleto set up something that needs to be created a new, or reset, for each test method.

使用@Rule设置的东西,需要创建一个新的,或重置,每个测试方法。

回答by Slavus

@Rule can not be set up to run before an @BeforeClass.

@Rule 不能设置为在 @BeforeClass 之前运行。

While @ClassRule must be on static method.

而@ClassRule 必须在静态方法上。

回答by Omprakash

Ref: Annotates static fields that reference rules or methods that return them. A field must be public, static, and a subtype of TestRule. A method must be public static, and return a subtype of TestRule.

Ref:注释引用规则或返回它们的方法的静态字段。字段必须是公共的、静态的,并且是 TestRule 的子类型。方法必须是公共静态的,并返回 TestRule 的子类型。

The Statement passed to the TestRule will run any BeforeClass methods, then the entire body of the test class (all contained methods, if it is a standard JUnit test class, or all contained classes, if it is a Suite), and finally any AfterClass methods.

传递给 TestRule 的 Statement 将运行任何 BeforeClass 方法,然后是测试类的整个主体(所有包含的方法,如果是标准 JUnit 测试类,或者所有包含的类,如果是套件),最后是任何 AfterClass方法。

https://junit.org/junit4/javadoc/4.12/org/junit/ClassRule.html

https://junit.org/junit4/javadoc/4.12/org/junit/ClassRule.html