[rest api 프로젝트 구축] - 예외 테스트 방법

geun kim·2023년 3월 27일
0

REST API 구축

목록 보기
12/20
  1. @Test(expected)
    예외 타입만 확인 가능
  2. try-catch
    예외 타입과 메시지 확인 가능.
    하지만 코드가 다소 복잡.
  3. @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);

    }
profile
Devops Load Map

0개의 댓글