问题描述
我想为大量代码编写测试用例,我想知道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:规则.
For more information see JUnit wiki : Rules.
这篇关于Junit @Rule 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!