Junit @Rule如何运作? [英] How does Junit @Rule work?
问题描述
我想为大量代码编写测试用例,我想知道JUnit @Rule
注释功能的详细信息,以便我可以用它来编写测试案例。请提供一些好的答案或链接,通过一个简单的例子详细说明其功能。
I want to write test cases for a bulk of code, I would like to know details of JUnit @Rule
annotation feature, so that I can use it for writing test cases. Please provide some good answers or links, which give detailed description of its functionality through a simple example.
推荐答案
规则用于添加其他功能适用于测试类中的所有测试,但是采用更通用的方式。
Rules are used to add additional functionality which applies to all tests within a test class, but in a more generic way.
例如, ExternalResource 在测试方法之前和之后执行代码,而不必使用 @Before
和 @After
。使用 ExternalResource
而不是 @Before
和 @After
给出更好的代码重用的机会;可以从两个不同的测试类中使用相同的规则。
For instance, ExternalResource executes code before and after a test method, without having to use @Before
and @After
. Using an ExternalResource
rather than @Before
and @After
gives opportunities for better code reuse; the same rule can be used from two different test classes.
该设计基于: JUnit中的拦截器
了解更多有关信息,请参阅 JUnit wiki:Rules 。
For more information see JUnit wiki : Rules.
这篇关于Junit @Rule如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!