aspose file tools*
The moose likes Testing and the fly likes how to mock return value of method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "how to mock return value of method" Watch "how to mock return value of method" New topic
Author

how to mock return value of method

irshad irshadoz
Greenhorn

Joined: Jan 16, 2013
Posts: 6
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

Joined: May 26, 2003
Posts: 30762
    
156

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:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
irshad irshadoz
Greenhorn

Joined: Jan 16, 2013
Posts: 6
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

Joined: May 26, 2003
Posts: 30762
    
156

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

Joined: Jan 16, 2013
Posts: 6
thanks a lot for the clear explanation problem resolved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to mock return value of method