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

  如果喜歡Hamcrest風(fēng)格的驗證風(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()));
    }
  第一種土鱉,第二種簡潔,第四種靠譜。

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