1

I'm using Spring Data Rest with a custom controller.

The custom controller overrides only the PATCH/PUT and POST verbs.

@ExtendWith(SpringExtension.class)
@WebMvcTest(UserController.class)
public class UserControllerTests {
    @Autowired
    MockMvc mockMvc;

    @Test
    public void updateTest() throws Exception {
        RequestBuilder request = MockMvcRequestBuilders
                .patch("/api/v1/users/1")
                .contentType(MediaType.APPLICATION_JSON)
                .content(updateString);
    
        MvcResult result = mockMvc.perform(request)
                .andExpect(status().isAccepted())
                .andExpect(content().json(updateString))
                .andReturn();
    }
}

For some reason mockMvc.perform always return a 404 but I expect a 204. How to fix this?

M. Justin
  • 14,487
  • 7
  • 91
  • 130
erotsppa
  • 14,248
  • 33
  • 123
  • 181
  • There aren't enough details to reproduce this scenario, but my guess is that "/api/v1/users/1" isn't actually the URL being wired up by the `UserController`. At a minimum, we'd need to see how the URL is being configured in the application. Also your `UserController` (with details simplified to a minimal reproducible example) would likely help. – M. Justin Sep 07 '21 at 15:33

0 Answers0