File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to mock return value of method

 
irshad irshadoz
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

i am new to unit testing, 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?

Any advice is most welcome.

Thnks in advacnce
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why the return type being the same matters at all. Is it possible you are really mocking B and not A?

I think your biggest problem is this line:


This doesn't let you inject Class A - so you can't inject your mock. Can you refactor to:

 
irshad irshadoz
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeanne,

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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
irshad irshadoz
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for the clear explanation problem resolved
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic