<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    上善若水
    In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
    posts - 146,comments - 147,trackbacks - 0
    說來慚愧,雖然之前已經看過JUnit的源碼了,也寫了幾篇博客,但是長時間不寫Test Case,今天想要寫拋Exception相關的test case時,竟然不知道怎么寫了。。。。。好記性不如爛筆頭,記下來先~~

    對于使用驗證Test Case方法中拋出的異常,我起初想到的是一種比較簡單的方法,但是顯得比較繁瑣:
        @Test
        
    public void testOldStyle() {
            
    try {
                
    double value = Math.random();
                
    if(value < 0.5{
                    
    throw new IllegalStateException("test");
                }

                Assert.fail(
    "Expect IllegalStateException");
            }
     catch(IllegalStateException e) {
            }

        }

    Google了一下,找到另外幾種更加方便的方法:1,使用Test注解中的expected字段判斷拋出異常的類型。2,使用ExpectedException的Rule注解。
    個人偏好用Test注解中的expected字段,它先的更加簡潔,不管讀起來還是寫起來都很方便,并且一目了然:
        @Test(expected = IllegalStateException.class)
        
    public void testThrowException() {
            
    throw new IllegalStateException("test");
        }

        
        @Test(expected 
    = IllegalStateException.class)
        
    public void testNotThrowException() {
            System.out.println(
    "No Exception throws");
        }

    對Rule注解的使用(只有在JUnit4.7以后才有這個功能),它提供了更加強大的功能,它可以同時檢查異常類型以及異常消息內容,這些內容可以只包含其中的某些字符,ExpectedException還支持使用hamcrest中的Matcher,默認使用IsInstanceOf和StringContains Matcher。在BlockJUnit4ClassRunner的實現中,每一個Test Case運行時都會重新創建Test Class的實例,因而在使用ExpectedException這個Rule時,不用擔心在多個Test Case之間相互影響的問題:
        @Rule
        
    public final ExpectedException expectedException = ExpectedException.none();
        
        @Test
        
    public void testThrowExceptionWithRule() {
            expectedException.expect(IllegalStateException.
    class);
            
            
    throw new IllegalStateException("test");
        }

        
        @Test
        
    public void testThrowExceptionAndMessageWithRule() {
            expectedException.expect(IllegalStateException.
    class);
            expectedException.expectMessage(
    "fail");
            
            
    throw new IllegalStateException("expect fail");
        }

    在stackoverflow中還有人提到了使用google-code中的catch-exception工程,今天沒時間看了,回去好好研究一下。地址是:http://code.google.com/p/catch-exception/

    posted on 2012-11-02 15:02 DLevin 閱讀(9657) 評論(0)  編輯  收藏 所屬分類: JUnit
    主站蜘蛛池模板: jizz日本免费| 亚美影视免费在线观看| 222www免费视频| 亚洲久本草在线中文字幕| 久久美女网站免费| 亚洲AV日韩AV高潮无码专区| 成人A片产无码免费视频在线观看| 国产成人麻豆亚洲综合无码精品| 久久久久久久国产免费看| 亚洲人JIZZ日本人| 成人免费区一区二区三区 | 亚洲av专区无码观看精品天堂 | 岛国精品一区免费视频在线观看 | 国产又黄又爽又大的免费视频| 中文字幕亚洲乱码熟女一区二区 | 精品国产污污免费网站入口在线| 亚洲一区二区三区乱码A| 丝袜足液精子免费视频| 亚洲AV成人片色在线观看| 69精品免费视频| 亚洲爆乳少妇无码激情| 久久久久亚洲av毛片大| 91禁漫免费进入| 亚洲欧好州第一的日产suv| 亚洲国产专区一区| 日本免费人成网ww555在线| 久久亚洲精品成人av无码网站| 欧美男同gv免费网站观看| 激情无码亚洲一区二区三区| 亚洲小说区图片区另类春色| 亚洲最大免费视频网| 亚洲爆乳少妇无码激情| 亚洲欧洲∨国产一区二区三区| 国产在线观看麻豆91精品免费| 亚洲国产成人无码AV在线影院 | 亚洲精品一卡2卡3卡三卡四卡| 成年大片免费视频| 岛国岛国免费V片在线观看| 亚洲人成电影在线观看网| 亚洲 综合 国产 欧洲 丝袜| 99在线观看视频免费|