This is my controller class. Now that i want to write unit test cases for the below controller class using mockito
can anyone help me out from this?
@Controller
public class LoginController {
@Autowired
@Qualifier("skillService")
private SkillService skillService;
@Autowired
private SkillReferenceData skillReferenceData;
@Autowired
private EmployeeValidator employeeValidator;
@RequestMapping(value = "/loginview.html", method = RequestMethod.GET)
@PreAuthorize("hasAuthority('ROLE_ANONYMOUS')")
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse respresultonse) throws Exception {
ModelAndView model = new ModelAndView("login");
return model;
}
@RequestMapping("/login.htm")
protected ModelAndView onSubmit(@ModelAttribute("userVB") UserVB userVB,
BindingResult result, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("login");
}
}
Create an instance of your Controller class by:
@InjectMocks
LoginController loginController;
By using this annotation you can also access and mock your private variables like skillService, skillReferenceData, employeeValidator by using:
@Mock(name = "skillService")
SkillService mockSkillService = createMock(SkillService.class);
Don't forget to initialize your Mockito annotations by adding MockitoAnnotations.initMocks(this);
before your unit tests.
Finally, you can mock your constructors by using:
Mockito.when(new ModelAndView(any(String.class).thenReturn(null)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.