i am new to unittesting, and i am using mockito for my unit test. My question is i am writing unit test for methodB as it can be shown below but it is dependent on a variable from methodA in order to complete the test. how can i mock valueMethodA in methodB?
i can mock the return value when the return type are the same but this case they are different, methodA returns an int type whereas methodB returns boolean. how to mock valueMethodA?
thanks a lot for your quick response..my apologies the example i gave above it was mainly to describe the scenario of my issue as i thought it was the return type issue. Please see the method which i want to carry out unit test for below:
My issue is that i don't know how to mock the boolean variable isUserValid? The variable isUserValid returns a boolean whereas my method validateViewLogin above returns a type ModelAndView. Basically i was trying to mock the isUserValid to return true so as to test the flow when the user is valid.
I was planning a unit test as shown below:
My assert will be if user validation is successful page login will be displayed
When i run my unit test i get this error message:
I know i am getting this error because
returns a boolean and my method expecting ModelAndView.How can i solve this to be able to carry out my unit test .
Any advice please?
This is my controller to validate a user when submitting a form with username, password and i am using spring framework.
thanks in advance
author & internet detective
You don't want to mock out the boolean. You want to mock the service that returns a value that the boolean is assigned to.
Your code is close. This part is good.
But then you don't actually set the mock call of userManagerService.validateUserLogin on mockpdao. Once you set this, that mock call returns a boolean and your code proceeds. Try replacing this line with one that sets the mockpdao.
You are trying to test the controller so you shouldn't mock it out here.
Joined: Jan 16, 2013
thanks a lot for the clear explanation problem resolved