网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

使用JavaJUnit框架里的@SuiteClasses注解管理测试用例

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.

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、网站设计、平川网络推广、小程序开发、平川网络营销、平川企业策划、平川品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供平川建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

The below class RepeatDemoOne is a bad example, where this special LOOP operation is mixed with test method implementation.

使用Java JUnit框架里的@SuiteClasses注解管理测试用例

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.

使用Java JUnit框架里的@SuiteClasses注解管理测试用例

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:

使用Java JUnit框架里的@SuiteClasses注解管理测试用例

With the help of @Rule, we can achieve the same as @Test(expected=).

使用Java JUnit框架里的@SuiteClasses注解管理测试用例

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.

使用Java JUnit框架里的@SuiteClasses注解管理测试用例

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()
        {
            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:

使用Java JUnit框架里的@SuiteClasses注解管理测试用例使用Java JUnit框架里的@SuiteClasses注解管理测试用例

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

使用Java JUnit框架里的@SuiteClasses注解管理测试用例


网页名称:使用JavaJUnit框架里的@SuiteClasses注解管理测试用例
文章起源:http://bjjierui.cn/article/jsoohc.html

其他资讯