spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.61k stars 38.13k forks source link

Spring Junit for controller [SPR-16370] #20917

Closed spring-projects-issues closed 6 years ago

spring-projects-issues commented 6 years ago

Akhila opened SPR-16370 and commented

Controller Method: @RequestMapping(value = "/eligibleAccounts", method = RequestMethod.POST) public ModelMap reguGetEligibleAccounts(@RequestBody @Valid AccountTypeRequest request, BindingResult bindingResult, HttpServletRequest servReq, ModelMap modelMap) {

    if (bindingResult != null && bindingResult.hasErrors()) {
        logger.error("eligibleAccounts :: error in binding request");
        modelMap.addAttribute(Message.MESSAGES, reguErrorHandler.getDefaultErrorMessageObject(servReq));
        return modelMap;
    }

    long userId = 0;
    try {
        userId = Long.valueOf(userSession.getSmUser());
    } catch (Exception e) {
        logger.error("eligibleAccounts :: error with getting userId");
        modelMap.addAttribute(Message.MESSAGES, reguErrorHandler.getDefaultErrorMessageObject(servReq)); 
        return modelMap;
    }

    ReguAccountDetails details;
    try {
        logger.info("eligible account service call starts !!!");
        details = reguService.getReguEligibleAccount(userId, request.getAccountType());
        logger.info("Mapping domans to display classes...!!");
        CustomerEligibleAccountDisplay eligibleAccount = reguViewHandler.getEligibleAccounts(details);
        modelMap.addAttribute("accounts",eligibleAccount);
        return modelMap;
    } catch (ReguException e) {
        logger.error("eligibleAccounts :: error with hitting service");
        modelMap.addAttribute(Message.MESSAGES, reguErrorHandler.getErrorMessageObject(e,servReq));
        return modelMap;
    }

}

For this controller method, I have to write Junit. I have written like:

@Test public void testReguEligigbleAccounts() { byte[] jsonContent = loadJson("classpath:/config/mock/eligibleAccount.json");

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/loc/eligibleAccounts");

    request.addHeader("Content-Type", "application/json");
    request.setContentType("application/json");
    request.setContent(jsonContent);

    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView            mv       = null;

    try
    {
      mv = annotationMethodHandlerAdapter.handle(request, response, controller);
    }
    catch (Exception exp)
    {
      log.error("annotationMethodHandlerAdapter.handle() thrown the exception!", exp);
      assertNull ("annotationMethodHandlerAdapter.handle() throws an exception: " + exp, exp);
    }

    assertNotNull("ModelAndView object is not returned by EligibleAccounts() action!", mv);

}

Error: Failed tests: ReguControllerTest.testReguEligigbleAccounts:190 annotationMethodHandlerAdapter.handle() throws an exception: org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.ui.ModelMap com.etrade.neo.regu.controller.ReguController.reguGetEligibleAccounts(com.etrade.neo.regu.request.AccountTypeRequest,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)]; nested exception is java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature! expected null, but was:<org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.ui.ModelMap com.etrade.neo.regu.controller.ReguController.reguGetEligibleAccounts(com.etrade.neo.regu.request.AccountTypeRequest,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap)]; nested exception is java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!>


No further details from SPR-16370

spring-projects-issues commented 6 years ago

Juergen Hoeller commented

I'm afraid this is not a bug report but rather a question for a forum or for StackOverflow. Please only report actual framework issues or improvement requests here, ideally tested against the most recent version.