코딩하는 털보

21.11.10 TIL 본문

Diary/Today I Learned

21.11.10 TIL

이정인 2021. 11. 11. 01:17

오늘의 삽질

Junit으로 Filter 테스트 하기

response, request, filterChain을 mock 객체로 생성하여
given()으로 filterChain.doFileter() 메서드를 mock 으로 응답받을 수 있다.

@ExtendWith(MockitoExtension.class)
class JwtExceptionHandlerFilterTest {

    HttpServletResponse response = null;
    HttpServletRequest request = null;
    FilterChain filterChain = null;
    PrintWriter mockWriter = null;

    @BeforeEach
    private void setUp() throws IOException {
        //given
        response = mock(HttpServletResponse.class);
        request = mock(HttpServletRequest.class);
        filterChain = mock(FilterChain.class);
        mockWriter = mock(PrintWriter.class);

        String clientRequestUri = "/test";
        String message = "test message";
        String body = null;
        String method = "GET";
        int code = 400;

        given(request.getAttribute("clientRequestUri")).willReturn(clientRequestUri);
        given(request.getAttribute("msg")).willReturn(message);
        given(request.getAttribute("messageBody")).willReturn(body);
        given(request.getAttribute("method")).willReturn(method);
        given(request.getAttribute("code")).willReturn(400);
        given(response.getWriter()).willReturn(mockWriter);
    }

    @DisplayName("Jwt ExpiredJwtException 처리")
    @Test
    void filterExpiredJwtException() throws ServletException, IOException {
        //given
        willThrow(ExpiredJwtException.class).given(filterChain).doFilter(request, response);

        //when
        JwtExceptionHandlerFilter jwtExceptionHandlerFilter = new JwtExceptionHandlerFilter();
        jwtExceptionHandlerFilter.doFilterInternal(request,response,filterChain);

        //then
        verify(filterChain).doFilter(request, response);
        verify(response.getWriter()).write(anyString());
    }

    @DisplayName("Jwt SignatureException 처리")
    @Test
    void filterSignatureException() throws ServletException, IOException {
        //given
        willThrow(SignatureException.class).given(filterChain).doFilter(request, response);

        //when
        JwtExceptionHandlerFilter jwtExceptionHandlerFilter = new JwtExceptionHandlerFilter();
        jwtExceptionHandlerFilter.doFilterInternal(request,response,filterChain);

        //then
        verify(filterChain).doFilter(request, response);
        verify(response.getWriter()).write(anyString());
    }

    @DisplayName("Jwt MalformedJwtException 처리")
    @Test
    void filterMalformedJwtException() throws ServletException, IOException {
        //given
        willThrow(MalformedJwtException.class).given(filterChain).doFilter(request, response);

        //when
        JwtExceptionHandlerFilter jwtExceptionHandlerFilter = new JwtExceptionHandlerFilter();
        jwtExceptionHandlerFilter.doFilterInternal(request,response,filterChain);

        //then
        verify(filterChain).doFilter(request, response);
        verify(response.getWriter()).write(anyString());
    }
}
Comments