2

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());
    }


}
AMITH SAI
  • 85
  • 2
  • 9
  • 1
    Please add some code on what you have tried so far in writing mockito test. what issue you faced ? – Ajit Soman Mar 20 '20 at 05:12
  • 1
    We can't just tell you how to "test this method". You need to learn how to test. SUGGESTION: Please consider working through any/all of these tutorials: https://www.baeldung.com/spring-boot-testing, https://spring.io/guides/gs/testing-web/, https://mkyong.com/spring-boot/spring-rest-integration-test-example/ – FoggyDay Mar 20 '20 at 05:15
  • mockMvc.perform(post("/updateDetails").accept(MediaType.TEXT_HTML).params(params)).andExpect(status().isOk()); – AMITH SAI Mar 20 '20 at 05:18
  • This says that post method is forbidden and my test fails – AMITH SAI Mar 20 '20 at 05:19
  • @AMITHSAI, are you able to hit successfully with postman ? – Ajit Soman Mar 20 '20 at 05:44

1 Answers1

3

Regarding your update:

  1. Thank you for clarifying your post with a specific error/specific problem.

  2. 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.

  1. Please - PLEASE - 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!

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • well i have autowired the webApplicationContext. Sorry i didn't post the entire code. But it still gives the same forbidden error. – AMITH SAI Mar 20 '20 at 06:16