亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-08-13 13:43:08 來源:ITPUB博客 閱讀:205 作者:i042416 欄目:編程語言

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.


使用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<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注解管理測試用例 使用Java JUnit框架里的@SuiteClasses注解管理測試用例


要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

高淳县| 福贡县| 克什克腾旗| 图片| 唐海县| 陈巴尔虎旗| 拜泉县| 巨鹿县| 昌吉市| 阆中市| 晋州市| 光山县| 佛学| 弋阳县| 乐都县| 土默特右旗| 志丹县| 南康市| 乌鲁木齐县| 蓝山县| 淄博市| 怀远县| 岱山县| 富锦市| 太谷县| 通辽市| 保定市| 峨眉山市| 钟山县| 马山县| 铜山县| 江城| 民勤县| 衡南县| 尼勒克县| 牟定县| 九江县| 台北市| 衢州市| 美姑县| 丽水市|