- @Test(expected)
예외 타입만 확인 가능
- try-catch
예외 타입과 메시지 확인 가능.
하지만 코드가 다소 복잡.
- @Rule ExpectedException
코드는 간결하면서 예외 타입과 메시지 모두 확인 가능
@Test(expected = UsernameNotFoundException.class)
public void findByUsernameFail(){
String username = "none@eamil.com";
accountService.loadUserByUsername("none@eamil.com");
}
@Test
public void findByUsernameFail2(){
String username = "none@eamil.com";
try{
accountService.loadUserByUsername("none@eamil.com");
fail("supposed to be failed");
}catch (UsernameNotFoundException e){
Assertions.assertThat(e.getMessage()).containsSequence(username);
}
}
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void findByUsernameFail3(){
//expected
String username = "none@eamil.com";
expectedException.expect(UsernameNotFoundException.class);
expectedException.expectMessage(Matchers.containsString(username));
//when
accountService.loadUserByUsername(username);
}