<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
    主站蜘蛛池模板: 亚洲精品乱码久久久久蜜桃| 精品亚洲国产成人| 久久久WWW成人免费精品| 亚洲精品亚洲人成在线观看下载| 日韩国产精品亚洲а∨天堂免| 成人免费视频软件网站| 亚洲hairy多毛pics大全| 国产午夜免费秋霞影院| 男女猛烈激情xx00免费视频| 久久久久噜噜噜亚洲熟女综合| 国产VA免费精品高清在线| 中文字幕一精品亚洲无线一区| 成在线人视频免费视频| 亚洲精品高清视频| 国产精品亚洲成在人线| 91精品全国免费观看青青| 亚洲日本中文字幕区| 成人免费的性色视频| 亚洲精品无码成人片久久不卡| 亚洲 无码 在线 专区| 91精品全国免费观看青青| 亚洲日本乱码一区二区在线二产线 | 精品久久久久久亚洲精品| 噼里啪啦电影在线观看免费高清 | 2020因为爱你带字幕免费观看全集 | 亚洲精品国产日韩| 中国极品美軳免费观看| 亚洲AV一宅男色影视| 1000部拍拍拍18免费网站| 亚洲成av人片在线天堂无| 不卡一卡二卡三亚洲| 亚洲高清视频免费| 色欲aⅴ亚洲情无码AV| 亚洲成Av人片乱码色午夜| 69成人免费视频无码专区| 人妻免费久久久久久久了| 亚洲欧洲综合在线| 亚洲A∨午夜成人片精品网站| 日本卡1卡2卡三卡免费| 亚洲妇女无套内射精| 亚洲AV日韩AV永久无码绿巨人|