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

  然后這樣書(shū)寫(xiě)測(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"));

    }
  這樣的好處是可以的驗(yàn)證異常是被測(cè)方法拋出來(lái)的,而不是其它方法拋出來(lái)的。

  catch-exception庫(kù)還提供了多種API來(lái)進(jìn)行測(cè)試。

  先加載fest-assertion庫(kù)。

        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert-core</artifactId>
            <version>2.0M10</version>
        </dependency>
  然后可以書(shū)寫(xiě)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();
    }

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