中文JAVA技術平等自由協作創造

          Java專題文章博客和開源

          常用鏈接

          統計

          最新評論

          Java中測試異常的多種方式

            使用JUnit來測試Java代碼中的異常有很多種方式,你知道幾種?
            給定這樣一個class.
            Person.java
            public class Person {
            private String name;
            private int age;
            public String getName() {
            return name;
            }
            public void setName(String name) {
            this.name = name;
            }
            public int getAge() {
            return age;
            }
            public void setAge(int age) {
            if (age < 0 ) {
            throw new IllegalArgumentException("age is invalid");
            }
            this.age = age;
            }
            }
            我們來測試setAge方法。
            Try-catch 方式
            @Test
            public void shouldGetExceptionWhenAgeLessThan0() {
            Person person = new Person();
            try {
            person.setAge(-1);
            fail("should get IllegalArgumentException");
            } catch (IllegalArgumentException ex) {
            assertThat(ex.getMessage(),containsString("age is invalid"));
            }
            }
            這是最容易想到的一種方式,但是太啰嗦。
            JUnit annotation方式托福答案 www.qcwy123.com
            JUnit中提供了一個expected的annotation來檢查異常。
            @Test(expected = IllegalArgumentException.class)
            public void shouldGetExceptionWhenAgeLessThan0() {
            Person person = new Person();
            person.setAge(-1);
            }
            這種方式看起來要簡潔多了,但是無法檢查異常中的消息。
            ExpectedException rule
            JUnit7以后提供了一個叫做ExpectedException的Rule來實現對異常的測試。
            @Rule
            public ExpectedException exception = ExpectedException.none();
            @Test
            public void shouldGetExceptionWhenAgeLessThan0() {
            Person person = new Person();
            exception.expect(IllegalArgumentException.class);
            exception.expectMessage(containsString("age is invalid"));
            person.setAge(-1);
            }
            這種方式既可以檢查異常類型,也可以驗證異常中的消息。
            使用catch-exception庫
            有個catch-exception庫也可以實現對異常的測試。
            首先引用該庫。
            pom.xml
            <dependency>
            <groupId>com.googlecode.catch-exception</groupId>
            <artifactId>catch-exception</artifactId>
            <version>1.2.0</version>
            <scope>test</scope> <!-- test scope to use it only in tests -->
            </dependency>
            然后這樣書寫測試。
            @Test
            public void shouldGetExceptionWhenAgeLessThan0() {
            Person person = new Person();
            catchException(person)。setAge(-1);
            assertThat(caughtException(),instanceOf(IllegalArgumentException.class));
            assertThat(caughtException()。getMessage(), containsString("age is invalid"));
            }
            這樣的好處是可以精準的驗證異常是被測方法拋出來的,而不是其它方法拋出來的。
            catch-exception庫還提供了多種API來進行測試雅思答案 www.tygj123.com
            先加載fest-assertion庫。
            <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert-core</artifactId>
            <version>2.0M10</version>
            </dependency>
            然后可以書寫BDD風格的測試。
            @Test
            public void shouldGetExceptionWhenAgeLessThan0() {
            // given
            Person person = new Person();
            // when
            when(person)。setAge(-1);
            // then
            then(caughtException())
            .isInstanceOf(IllegalArgumentException.class)
            .hasMessage("age is invalid")
            .hasNoCause();
            }
            如果喜歡Hamcrest風格的驗證風格的話,catch-exception也提供了相應的Matcher API.
            @Test
            public void shouldGetExceptionWhenAgeLessThan0() {
            // given
            Person person = new Person();
            // when
            when(person)。setAge(-1);
            // then
            assertThat(caughtException(), allOf(
            instanceOf(IllegalArgumentException.class)
            , hasMessage("age is invalid")
            ,hasNoCause()));
            }
            第一種最土鱉,第二種最簡潔,第四種最靠譜。

          posted on 2014-04-20 09:20 好不容易 閱讀(203) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 乌兰浩特市| 闽清县| 云安县| 东丰县| 山西省| 金乡县| 繁昌县| 姚安县| 锡林浩特市| 金阳县| 黄陵县| 延边| 景泰县| 汾阳市| 固阳县| 城步| 礼泉县| 上思县| 苏州市| 宁武县| 大厂| 湘潭市| 凉城县| 临潭县| 海南省| 蒙山县| 木里| 双牌县| 崇文区| 老河口市| 高碑店市| 南康市| 米林县| 固阳县| 永福县| 咸阳市| 台东县| 伊吾县| 通州区| 青铜峡市| 阿拉善右旗|