일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 합병 정렬
- auto.create.topics.enable
- Switch Expressions
- 자바할래
- 항해99
- 접근지시자
- 제네릭 타입
- 상속
- 스파르타코딩클럽
- System.out
- 브릿지 메소드
- docker
- System.err
- System.in
- 바운디드 타입
- annotation processor
- 프리미티브 타입
- 익명 클래스
- Study Halle
- 자바스터디
- 정렬
- 로컬 클래스
- yield
- github api
- junit 5
- raw 타입
- 제네릭 와일드 카드
- throwable
- 람다식
- 함수형 인터페이스
Archives
- Today
- Total
코딩하는 털보
21.11.10 TIL 본문
오늘의 삽질
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