中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

          Java專題文章博客和開源

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Java中測(cè)試異常的多種方式

            使用JUnit來測(cè)試Java代碼中的異常有很多種方式,你知道幾種?
            給定這樣一個(gè)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;
            }
            }
            我們來測(cè)試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中提供了一個(gè)expected的annotation來檢查異常。
            @Test(expected = IllegalArgumentException.class)
            public void shouldGetExceptionWhenAgeLessThan0() {
            Person person = new Person();
            person.setAge(-1);
            }
            這種方式看起來要簡(jiǎn)潔多了,但是無法檢查異常中的消息。
            ExpectedException rule
            JUnit7以后提供了一個(gè)叫做ExpectedException的Rule來實(shí)現(xiàn)對(duì)異常的測(cè)試。
            @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);
            }
            這種方式既可以檢查異常類型,也可以驗(yàn)證異常中的消息。
            使用catch-exception庫
            有個(gè)catch-exception庫也可以實(shí)現(xiàn)對(duì)異常的測(cè)試。
            首先引用該庫。
            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>
            然后這樣書寫測(cè)試。
            @Test
            public void shouldGetExceptionWhenAgeLessThan0() {
            Person person = new Person();
            catchException(person)。setAge(-1);
            assertThat(caughtException(),instanceOf(IllegalArgumentException.class));
            assertThat(caughtException()。getMessage(), containsString("age is invalid"));
            }
            這樣的好處是可以精準(zhǔn)的驗(yàn)證異常是被測(cè)方法拋出來的,而不是其它方法拋出來的。
            catch-exception庫還提供了多種API來進(jìn)行測(cè)試雅思答案 www.tygj123.com
            先加載fest-assertion庫。
            <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert-core</artifactId>
            <version>2.0M10</version>
            </dependency>
            然后可以書寫B(tài)DD風(fēng)格的測(cè)試。
            @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風(fēng)格的驗(yàn)證風(fēng)格的話,catch-exception也提供了相應(yīng)的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()));
            }
            第一種最土鱉,第二種最簡(jiǎn)潔,第四種最靠譜。

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


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          PK10開獎(jiǎng) PK10開獎(jiǎng)
          主站蜘蛛池模板: 临清市| 新龙县| 堆龙德庆县| 新营市| 开阳县| 蒙城县| 喀喇沁旗| 凉山| 巨野县| 腾冲县| 赞皇县| 友谊县| 临澧县| 武陟县| 东阳市| 永嘉县| 锡林浩特市| 灵山县| 宜城市| 白山市| 家居| 益阳市| 福清市| 皮山县| 巩义市| 玉树县| 邹平县| 鄯善县| 临朐县| 鸡泽县| 天门市| 区。| 辽阳县| 武穴市| 澄迈县| 桃园市| 铁岭市| 西乌珠穆沁旗| 乌苏市| 哈巴河县| 双江|