
given(oAuthLoginService.login(any(NaverUserDto.class))).willReturn(response);
MockMvc로 API 요청할 때, 응답 값이 비어있는 오류가 발생하였다.
Mocking한 Service 계층의 객체도 위 코드처럼 given()에 any()를 인자로 넣어 잘 구현했는데도 오류가 발생하였다.
given()이나when()으로 Mocking한 객체의 행동을 표현할 때, 인자에 객체를 넣으면 주소값이 서로 달라져 정해논 리턴값이 안 나올 수 있다. (아래 코드 참고!)
// request처럼 객체를 넣으면 안됨!
given(oAuthLoginService.login(request)).willReturn(response);
when(oAuthLoginService.login(request)).thenReturn(response);
// any()를 인자로 넣어 인자로 들어가는 클래스만 판단하도록 설정해야 함!
given(oAuthLoginService.login(any(NaverUserDto.class))).willReturn(response);
when(oAuthLoginService.login(any(NaverUserDto.class))).thenReturn(response);
@BeforeEach
void setUp(@Autowired RestDocumentationContextProvider restDocumentation,
@Autowired WebApplicationContext webApplicationContext) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation))
.build();
}
해결된 이유는 정확히 알 수 없었지만, Spring REST Docs로 API 문서를 만드는 중에 발생하여 Spring REST Docs의 공식 레퍼런스에 나온대로 위 코드를 추가하니 해결되었다.
혹시 왜 해결됐는지 알고 계신 분은 알려주세요...!
이걸로 하루종일 여러 블로그나 StackOverflow 글을 구글링 하며 삽질했었다...
결국 당일에 못찾아서 자고 난 뒤, 다음 날에 공식 문서에 나온 코드대로 다시 해봤더니 해결했다!
이제는 에러가 발생하면 공식 문서도 찾아봐야겠다!