Java中測試異常的多種方式(4)
發表于:2014-04-14來源:博客園作者:黃博文點擊數:
標簽:軟件測試
如果喜歡Hamcrest風格的驗證風格的話,catch-exception也提供了相應的Matcher API。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Test public void shouldGetExceptionWhenAgeLessThan0 () { // given
如果喜歡Hamcrest風格的驗證風格的話,catch-exception也提供了相應的Matcher API。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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()));
}
|
第一種最土鱉,第二種最簡潔,第四種最靠譜。
原文轉自:http://www.cnblogs.com/huang0925/p/3663074.html