<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
    說(shuō)來(lái)慚愧,雖然之前已經(jīng)看過(guò)JUnit的源碼了,也寫了幾篇博客,但是長(zhǎng)時(shí)間不寫Test Case,今天想要寫拋Exception相關(guān)的test case時(shí),竟然不知道怎么寫了。。。。。好記性不如爛筆頭,記下來(lái)先~~

    對(duì)于使用驗(yàn)證Test Case方法中拋出的異常,我起初想到的是一種比較簡(jiǎn)單的方法,但是顯得比較繁瑣:
        @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注解。
    個(gè)人偏好用Test注解中的expected字段,它先的更加簡(jiǎn)潔,不管讀起來(lái)還是寫起來(lái)都很方便,并且一目了然:
        @Test(expected = IllegalStateException.class)
        
    public void testThrowException() {
            
    throw new IllegalStateException("test");
        }

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

    對(duì)Rule注解的使用(只有在JUnit4.7以后才有這個(gè)功能),它提供了更加強(qiáng)大的功能,它可以同時(shí)檢查異常類型以及異常消息內(nèi)容,這些內(nèi)容可以只包含其中的某些字符,ExpectedException還支持使用hamcrest中的Matcher,默認(rèn)使用IsInstanceOf和StringContains Matcher。在BlockJUnit4ClassRunner的實(shí)現(xiàn)中,每一個(gè)Test Case運(yùn)行時(shí)都會(huì)重新創(chuàng)建Test Class的實(shí)例,因而在使用ExpectedException這個(gè)Rule時(shí),不用擔(dān)心在多個(gè)Test Case之間相互影響的問(wèn)題:
        @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工程,今天沒(méi)時(shí)間看了,回去好好研究一下。地址是:http://code.google.com/p/catch-exception/

    posted on 2012-11-02 15:02 DLevin 閱讀(9665) 評(píng)論(0)  編輯  收藏 所屬分類: JUnit
    主站蜘蛛池模板: 综合在线免费视频| 永久在线毛片免费观看| 国产hs免费高清在线观看| 国产亚洲一区二区在线观看| 亚洲欧洲另类春色校园网站| 一级毛片无遮挡免费全部| 99在线热视频只有精品免费| 国产成人青青热久免费精品| 久久精品国产亚洲av影院| 最新亚洲人成网站在线观看| 香港a毛片免费观看| 免费一级国产生活片| 日本久久久久亚洲中字幕| 理论片在线观看免费| www视频在线观看免费| 国产精品亚洲产品一区二区三区| 亚洲午夜久久久精品电影院| 国产美女视频免费观看的网站| 久久受www免费人成_看片中文| 在线观看亚洲精品福利片| 亚洲日韩国产AV无码无码精品| 搡女人免费免费视频观看| 免费网站看v片在线香蕉| 亚洲久本草在线中文字幕| 免费精品视频在线| 国产福利在线免费| 亚洲成AV人在线播放无码| 国产91成人精品亚洲精品| 国产成在线观看免费视频| 亚洲国产另类久久久精品黑人| 亚洲 欧洲 日韩 综合在线| 色www永久免费| 国产中文字幕免费| 亚洲黄页网在线观看| 日本在线看片免费人成视频1000| 亚洲精品国产日韩无码AV永久免费网| 亚洲伊人色一综合网| 免费A级毛片无码视频| 区久久AAA片69亚洲| 免费看一级一级人妻片| 在线成人a毛片免费播放|