JUnit 註解@Rule的工作原理
Suppose you need to repeatedly execute some test method in your unit test case, for example, you would like to test getPrice based on the first set of test data 5 times in test method test1() while for the second set of test data, only one time should be executed.
The below class RepeatDemoOne is a bad example, where this special LOOP operation is mixed with test method implementation.
Ideally the test method should only contain the pure logic to operate on the method being tested. So we have a better solution RepeatDemoTwo:
It could easily be observed that now the test method test1 and test2 are rather clean: no more for LOOP and System.out.println exist any more.
Instead, I put the LOOP logic and print out operation into class RepeatableRule which implements interface MethodRule. The concrete rule implementation is done by overriding method apply as below:
class RepeatableRule implements MethodRule{
int times = 1;
String[] testMethods = null;
RepeatableRule(int times, String[] testMethods){
this.times = times;
this.testMethods = testMethods;
}
@Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
int loopTime = 1;
if(Arrays.asList(testMethods).contains(method.getName())) {
loopTime = times;
}
for(int i = 0; i < loopTime; i++ ) {
base.evaluate();
System.out.println(method.getName() + " executed.");
}
}
};
} }
When I execute this test case, I can get exactly the same result as RepeatDemoOne:
With the help of @Rule, we can achieve the same as @Test(expected=).
For example, we can use an instance of class ExpectedException to manually declare within a test method itself that a test method expects a given type of exception class.
Besides exception, we can also manually specify a sub string which is expected to appear in an error message, and add our custom error message in Junit report if a test method fails. See following code for example:
public class RuleWithException {
@Rule
public ExpectedException exp = ExpectedException.none();
@Test
public void expectMessage()
{
exp.expectMessage("Hello World");
throw new RuntimeException("Hello World will throw exception.");
}
@Test
public void expectCourse()
{
exp.expectCause(new BaseMatcher<IllegalArgumentException>()
{
public boolean matches(Object item)
{
return item instanceof IllegalArgumentException;
}
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("Expected exception with type IllegalArgumentException "
+ "raised in test method! ");
}
});
Throwable cause = new IllegalArgumentException("Cause Test.");
throw new RuntimeException(cause);
}}
In this example, if we comment out line 46, the customed message defined in method describeTo will be printed out in JUnit console:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2722718/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JUnit 註解@RunWith的工作原理
- JUnit 註解@Category的工作原理Go
- JUnit 註解@SuiteClasses的工作原理UI
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- 使用Java JUnit框架裡的@Rule註解的用法舉例Java框架
- Angular @Injectable 註解的工作原理淺析Angular
- 安卓單元測試 (八):Junit Rule 的使用安卓
- Spring框架裡註解@Autowired的工作原理Spring框架
- JUnit5註解學習指引
- SAP Fiori @OData.publish 註解的工作原理解析
- 暑期自學 Day 07 | Junit,反射,註解(一)反射
- 暑期自學 Day 08 | Junit,反射,註解(二)反射
- 在JUnit中使用@Rule測試檔案和目錄
- SAP Cloud for Customer Rule Editor的使用方法和底層工作原理Cloud
- Spring系列之新註解配置+Spring整合junit+註解注入Spring
- Transaction註解原理
- Angular @Inject 註解的實際應用例子和工作原理淺析Angular
- SAP Fiori 註解 @ObjectModel.readOnly工作原理解析Object
- 使用Java JUnit框架裡的@SuiteClasses註解管理測試用例Java框架UI
- @LoadBalanced註解原理
- JAVA 註解的基本原理Java
- Java註解與原理分析Java
- 深入瞭解Azure 機器學習的工作原理機器學習
- Nginx的工作原理和配置詳解Nginx
- spring JUnit 基本原理Spring
- SpringMVC工作原理詳解SpringMVC
- JUnit5學習之五:標籤(Tag)和自定義註解
- 代理伺服器的工作原理詳解伺服器
- SAP Fiori程式設計模型規範裡註解 - @OData.publish工作原理解析程式設計模型
- Struts2工作原理(圖解)圖解
- 交換機的作用、功能和工作原理詳解
- 解讀MySQL的InnoDB引擎日誌工作原理MySql
- Mirror 的工作原理
- Spark的工作原理Spark
- View的工作原理View
- HashMap的工作原理HashMap
- DHCP的工作原理
- tcmalloc的工作原理