您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Java中測試異常的多種方式
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/7/28 16:22:47 ] 推薦標(biāo)簽:開源單元測試

  然后這樣書寫測試。

    @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來進(jìn)行測試。

  先加載fest-assertion庫。

        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert-core</artifactId>
            <version>2.0M10</version>
        </dependency>
  然后可以書寫B(tài)DD風(fēng)格的測試。

    @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();
    }

上一頁1234下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd