JUnit 注解@Rule的工作原理-阿里云开发者社区

开发者社区> 开发者小助手-bz4> 正文

JUnit 注解@Rule的工作原理

简介: 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.


image.pngIdeally 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.

image.pngInstead, 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:image.pngWith the help of @Rule, we can achieve the same as @Test(expected=).image.pngFor 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.



image.pngBesides 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:image.pngimage.png

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
2315
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载