I'm not familiar with BDD style Mockito, but I'm guessing that the line
given(controller.validateViewLogin(person, result, request)).willReturn(mav);
means that you are asking the controller to return the given model and view whenever the validateViewLogin
method is called with the specified person, result and request. However the controller is not a mock, so this may be what is causing your error. What you should be doing instead is specifying the behaviour of how your mock user manager service should behave.
I notice that you are creating a mock of the UserManagerServiceImpl
class. Given that it ends with 'Impl' I am guessing that there is a correspondng UserManagerService
interface that you could mock instead. Mocktio can mock concrete classes, but it can not do this as easily as mocking an interface. Therefore if there is indeed an interface then I would mock that instead just to be safe.
You are injecting your mock using ReflectionTestUtils
. This probably isn't the cause of your error, but if it is possible for you to do so then I'd recommend adding a public setter to your controller to inject it more safely and easily.
Taking the above points, I would write your test like the following:
@Test public void validateViewLogin_validLogin_returnsHomePage() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
Person person = new Person();
person.setFirstName("John");
person.setPassword("123");
LogInController controller = new LogInController();
UserManagerService mockUserService = mock(UserManagerService.class);
// Configure mock user service to accept the person
when(mockUserService.validateUserLogin("John", "123")).thenReturn(true);
// Inject mock user service into controller
controller.setUserManagerService(mockUserService);
// Attempt the validation
ModelAndView mav =
controller.validateViewLogin(person, result, request);
// Check the result
assertEquals("home", mav.getViewName());
}
Since I'm not familiar with the BDD syntax I have configured the mock using the line
when(mockUserService.validateUserLogin("John", "123")).thenReturn(true);
but I assume that this is equivalent to
given(mockUserService.validateUserLogin("John", "123")).willReturn(true);