• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to mock return value of method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for the clear explanation problem resolved
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic