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
JUnit @Before vs @Rule
提问by kgf3JfUtW
I understand that,
我明白那个,
@Before
and@BeforeClass
run before each test, or the entire test class, respectively@Rule
and@ClassRule
wraps 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 @Before
and @Rule
? Under what conditions is one preferred over another?The same question also goes for @BeforeClass
vs.@ClassRule
.
我如何决定使用@Before
和@Rule
?在什么条件下一个人比另一个人更受欢迎?同样的问题也适用于@BeforeClass
vs. @ClassRule
。
采纳答案by Quwin
In order to use @Rule
, you require a class that implements TestRule
(preferred) or MethodRule
, as can be read here.
Whereas @Before
and @After
require a new method to be written in every test case, @Rule
does not because it is only an instantiation of already existing code.
为了使用@Rule
,您需要一个实现TestRule
(首选) or 的类MethodRule
,可以在此处阅读。而@Before
和@After
需要一种新的方法在每个测试用例来写,@Rule
不会因为这只是一个已经存在的代码的实例。
So, if you would use @Before
and @After
for setUp()
and tearDown()
that you'll be using in many test cases, it is actually a better idea to use @Rule
because of code reuse
. If you have a test case that requires a unique @Before
and/or @After
, then these annotations are preferable.
所以,如果你会使用@Before
,并@After
为setUp()
和tearDown()
,你会使用在很多测试案例,它实际上是一个更好的主意,利用@Rule
因code 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 文档,
TestRule
can do everything that could be done previously with methods annotated with@Before
,@After
,@BeforeClass
, or@AfterClass
, butTestRule
s are (1) more powerful, and (2) more easily shared between projects and classes.
TestRule
可以完成以前使用@Before
,@After
,@BeforeClass
, 或 注释的方法可以完成的所有事情@AfterClass
,但是TestRule
s (1) 更强大,并且(2) 更容易在项目和类之间共享。
Ways that TestRule
s are more powerful:
方法是TestRule
s为更强大:
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 methodExpectedException
: make flexible assertions about thrown exceptionsExternalResource
: start and stop a server, for exampleTemporaryFolder
: create fresh files, and delete after testTestName
: remember the test name for use during the methodTestWatcher
: add logic at events during method executionTimeout
: cause test to fail after a set timeVerifier
: 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 RuleChain
to specify the order in which the rules should be run.
规则的另一个好处是可以在单个测试用例中使用多个规则。您可能希望使用RuleChain
来指定规则运行的顺序。