-
InvalidUseOfMatchersException프레임워크/JUnit 2024. 3. 27. 09:05
테스트 작성하면서 발생한 에러 중 종종 만나는 예외를 정리해봅니다.
mockMvc를 사용하고 있고 아래와 같은 헬퍼함수를 하나 작성해서 공통으로 사용 중에 있습니다.
private ResultActions patchAndVerify(String id, String body) throws Exception { return mockMvc.perform(patch(baseUrl + "/" + id) .contentType(MediaType.APPLICATION_JSON) .content(body) ); }
수정하기 api의 컨트롤러에 대한 테스트를 작성하고 있습니다.
when() 절에서 any(JobPostingEditReq.class)를 사용하기 때문에 입력에 따른 결과값의 형태는 serviceTest에거 검증하려고 합니다.
@Test public void 요청_데이터가_유효한_경우_수정된_채용공고와_200_응답을_반환() throws Exception { String matchId = "1"; Map<String, String> validRequest = Map.of("title", defaultRequest.get("title")); String validBody = objectMapper.writeValueAsString(validRequest); when(jobPostingService.editJobPosting(Long.parseLong(matchId), any(JobPostingEditReq.class))) .thenReturn(jobPostingDto); patchAndVerify(matchId, validBody) .andExpect(status().isOk()) .andExpect(jsonPath("$.result").isNotEmpty()) .andExpect(jsonPath("$.error").isEmpty());; }
기존에는 when절에 any(JobPostingEditReq.class)만 인자로 전달하고 있었는데, Long.parseLong(matchId)를 추가하니
InvalidUseOfMatchersException 이 발생했습니다.
살펴보면 2개의 matcher 사용을 예상하는개 1개만 사용된 것이 문제인 듯 합니다.
최초에 any matcher를 사용하고 있었고 인자를 추가로 넣으면서 id를 전달하는 상황이었는데 id는 matcher의 형태로 전달하지 않고 value를 전달해서 발생한 이슈입니다. 따라서 id도 matcher 형태로 전달해봅니다.
감사합니다.
'프레임워크 > JUnit' 카테고리의 다른 글
내부에서 실행되는 메소드에 인자가 잘 전달되는지 테스트 (0) 2024.04.17 argThat을 사용해서 primitive 타입을 확인하려고 할 때 주의사항 (0) 2024.04.17 멀티 쓰레드 환경에서 게시글 조회수 증가에 대한 검증 (0) 2024.04.15