Can anyone asssist how to write mockMVC for the controller? I have come up with mockMVC however i am stuck and not sure how to test further
From UI
- User selectes from the drop down and does the form submit with select ID as name (which is @RequestBody String name in the controller)
My Controller
@PostMapping("/api/user")
public User getSearch(@RequestBody String name) {
User user=new User();
String result=userService.findByUser(name);
user.setUsername(result);
return user;
}
My Mock MVC Class
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Test
public void testUser() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(post("/api/user")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
Any help will be great