package com.atypon.task1; import com.atypon.task1.controllers.PostsController; import…
- User
package com.atypon.task1;
import com.atypon.task1.controllers.PostsController; import com.atypon.task1.services.LocatorService;
import com.atypon.task1.utils.RequestUtils; import jakarta.servlet.http.HttpServletRequest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(PostsController.class) class PostsControllerTests {
code@Autowired private MockMvc mockMvc; @MockBean private LocatorService locatorService; @Test void getAddressWithoutThrottling() throws Exception { String uri = "/api/posts"; HttpServletRequest request = RequestUtils.generateMockAtyponRequest(uri); mockMvc.perform(get(uri).requestAttr("jakarta.servlet.http.HttpServletRequest", request)) .andExpect(status().isOk()) .andExpect(content().string("http://atypon.com/api/posts")); verify(locatorService, times(1)).getUniformResourceLocator(request); }}
codepublic static HttpServletRequest generateMockAtyponRequest(String uri) { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); when(request.getScheme()).thenReturn("http"); when(request.getHeader("Host")).thenReturn("atypon.com"); when(request.getRequestURI()).thenReturn(uri); return request; }@RequestMapping("/api/posts") @RestController @RequiredArgsConstructor public class PostsController {
code - Assistant
This error happens because the controller method
getAddress()is not being called. The reason behind that is the mocked HttpServletRequest object is not set up correctly in the test class, which is why the value for the Host header is not being retrieved correctly in the controller.To fix this, update the
generateMockAtyponRequest()method in yourRequestUtilsclass as follows:javapublic static HttpServletRequest generateMockAtyponRequest(String uri) { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); when(request.getScheme()).thenReturn("http"); when(request.getHeader(eq("Host"))).thenReturn("atypon.com"); when(request.getRequestURI()).thenReturn(uri); return request; }You need to add
eq("Host")to thewhen(request.getHeader(...)), so that the mocked request returns the value as expected.Now, your test should work as expected.
- User
still
- Assistant
I apologize for the confusion. Let me provide another approach. Instead of mocking
HttpServletRequest, you can change your test method to use therequest()method in yourmockMvc.perform()statement.- Remove the
generateMockAtyponRequest()method completely. - Update your test method as follows:
java@Test void getAddressWithoutThrottling() throws Exception { String uri = "/api/posts"; mockMvc.perform(get(uri).header("Host", "atypon.com")) .andExpect(status().isOk()) .andExpect(content().string("http://atypon.com/api/posts")); verify(locatorService, times(1)).getUniformResourceLocator(any(HttpServletRequest.class)); } - Remove the