Reputation: 95
This function is used to update the user details in the database. can someone help me to write test cases for this function.
@RequestMapping(value = "/updateDetails", method = RequestMethod.POST)
public String updateVendorDetails(@Valid @ModelAttribute("users") Users users, BindingResult result,Model model) {
logger.info("{}.{}",new VendorController().getClass().getPackageName(), new VendorController().getClass().getName());
if(result.hasErrors()) {
model.addAttribute("edit","edit");
logger.warn("Function: updateVendorDetails(), Information: Error while updating vendor details");
return register.toString();
}
userDao.updateVendorDetails(users);
logger.info("Function: updateVendorDetails(), Information: Vendor details updated successfully");
return vendor.toString();
}
Update
Code:
mockMvc.perform(post("/updateDetails").accept(MediaType.TEXT_HTML).params(params)).andExpect(status().isOk());
Resulting error:
This says that post method is forbidden and my test fails
This is my Test class
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestVendorPage {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}
@WithMockUser(roles = "VENDOR")
@Test
public void testIfUpdateEdtailsIsAvailableOnlyForVendor() throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("firstName", "vinod");
params.add("lastName", "babu");
params.add("contactNumber", "9952016709");
mockMvc.perform(post("/updateDetails").accept(MediaType.TEXT_HTML).params(params)).andExpect(status().isOk());
}
}
Upvotes: 2
Views: 1050
Reputation: 121649
Regarding your update, for that specific error: HTTP 403: Forbidden
, this should resolve the problem:
Unit test Springboot MockMvc returns 403 Forbidden
i think probleam is happend in "mockMvc" object is not autowired.mockMvc object should load from WebApplicationContext in before program run.
Consider looking at one or more of the links I cited above.
I've found all three sites very valuable resources. Time spent with these tutorials will help you a great deal!
Upvotes: 2