使用JavaJUnit框架里的@SuiteClasses注解管理測(cè)試用例

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.

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、平川網(wǎng)絡(luò)推廣、小程序開發(fā)、平川網(wǎng)絡(luò)營(yíng)銷、平川企業(yè)策劃、平川品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供平川建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.muchs.cn

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

使用Java JUnit框架里的@SuiteClasses注解管理測(cè)試用例

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注解管理測(cè)試用例

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注解管理測(cè)試用例

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

使用Java JUnit框架里的@SuiteClasses注解管理測(cè)試用例

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注解管理測(cè)試用例

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:

使用Java JUnit框架里的@SuiteClasses注解管理測(cè)試用例使用Java JUnit框架里的@SuiteClasses注解管理測(cè)試用例

要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙":

使用Java JUnit框架里的@SuiteClasses注解管理測(cè)試用例

網(wǎng)站標(biāo)題:使用JavaJUnit框架里的@SuiteClasses注解管理測(cè)試用例
當(dāng)前URL:http://www.muchs.cn/article42/jsoohc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站建設(shè)外貿(mào)建站、微信公眾號(hào)、響應(yīng)式網(wǎng)站企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)