Skip to content

Spring Boot 3.n NoResourceFoundException 단위테스트 구현 #123

@sudo-Terry

Description

@sudo-Terry

배경

  • Spring Boot 3.x(Spring 6.x) 에서는 기존에 DispatchServlet이 Resouce matching이 되지 않는 경우에서 NoResourceFoundException를 던짐
  • Spring 5.x 기준으로는 같은 경우에서 NoHandlerFoundException을 던짐
  • 현재 GlobalExceptionHandler에서는 NoResourceFoundException를 잡아 메시지를 던지도록 처리되어 있고, 실제로 잘 처리하지만, 테스트 환경에서는 테스트가 통과가 되지 않음
    @Test
    @DisplayName("NoResourceFoundException 처리 테스트")
    void handleNoResourceFoundException() throws Exception {
        // given & when & then
        mockMvc.perform(get("/non-existent-endpoint"))
                .andExpect(status().isNotFound())
                .andExpect(jsonPath("$.code").value("C003"))
                .andExpect(jsonPath("$.status").value(404))
                .andExpect(jsonPath("$.message").value("요청하신 페이지나 기능을 찾을 수 없습니다."));
    }
  • 테스트가 실패하는 원인은 테스트 환경에서 /non-existent-endpoint으로 요청이 날라왔을 때, NoHandlerFoundException가 반환됨
  • standaloneSetup MockMvc 환경이 문제인걸까? 자세한 확인이 필요하다

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededinvalidThis doesn't seem right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions