Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Junit for controller [SPR-16370] #20917

Closed
spring-projects-issues opened this issue Jan 12, 2018 · 1 comment
Closed

Spring Junit for controller [SPR-16370] #20917

spring-projects-issues opened this issue Jan 12, 2018 · 1 comment
Labels
status: invalid

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jan 12, 2018

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
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 12, 2018

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.

@spring-projects-issues spring-projects-issues added type: bug status: invalid and removed type: bug labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid
Projects
None yet
Development

No branches or pull requests

1 participant