????JUnit annotation???
????JUnit?????????expected??annotation?????????

 

@Test(expected = IllegalArgumentException.class)
public void shouldGetExceptionWhenAgeLessThan0() {
Person person = new Person();
person.setAge(-1);
}

?????????????????????????????????????е??????
????ExpectedException rule
????JUnit7??????????????ExpectedException??Rule??????????????

 

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

???????????????????????????????????е??????
???????catch-exception??
?????и?catch-exception???????????????????
??????????????
????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>
?????????????д?????
????@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?????в????
?????????fest-assertion??
????<dependency>
????<groupId>org.easytesting</groupId>
????<artifactId>fest-assert-core</artifactId>
????<version>2.0M10</version>
????</dependency>