上善若水
          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 閱讀(9663) 評(píng)論(0)  編輯  收藏 所屬分類: JUnit
          主站蜘蛛池模板: 通道| 体育| 兴仁县| 彰化县| 克东县| 西和县| 林州市| 诸暨市| 博爱县| 武功县| 墨竹工卡县| 泗水县| 昭觉县| 英德市| 易门县| 蒙山县| 濉溪县| 吉林市| 宾阳县| 赣榆县| 高碑店市| 海兴县| 湄潭县| 天峨县| 岢岚县| 会泽县| 黑龙江省| 梁河县| 东光县| 阜新市| 铜川市| 库伦旗| 民县| 城口县| 海兴县| 集安市| 九江县| 义马市| 隆回县| 苍山县| 信丰县|