This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

error saying expected true, returned false despite setting thenRetun true

 
Ranch Hand
Posts: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have the below code using Junit and Mockito:



It is giving assertion failure. I have set   so am expecting that when databaseMock.isAvailable() will be called it will return true but it is returning false and the assertion is failing. Why is it not returning true?
thanks
 
Sheriff
Posts: 22584
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:


How does that even compile, if the Service class has only one constructor that takes a Database? Did you add that constructor after you wrote the test? Because then the Service instance uses its own Database that it creates itself.
 
Monica Shiralkar
Ranch Hand
Posts: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Below is the code which I am trying (without specifying any constructor )

 
Rob Spoor
Sheriff
Posts: 22584
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the Service creates and contains its own Database instance. That's not the mock you create.
 
Monica Shiralkar
Ranch Hand
Posts: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking that if we have to mock any method of any class all we have to do is first declare variable of that type with mock annotation like this :



And then specify in the when-thenReturns format that when this method is called then return this.



I thought thats all what is required. But now I came to know that that's not all what is required and that's something else is also required apart from this. I will read further and understand what else is required for this.

Thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the issue.

My code is simply calling the method using the database object and not the mockDatabase object, and thus not returning based on the mock behaviour (and instead returning false the default value of boolean ).

I added constructor to set this.database = mockObject , then passed the mockObject and it worked as expected.

Thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 2562
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is mocking a method call possible if the object on which the mocking method  call is made is not even member variable of that class ?


I had a field like this in the class whose method was being tested :



And it was getting set in  constructor like this :



Then we could mock the below call for available method:



Is it possible to mock some call if the object using which the external method is called is not even member of the class and thus can't be set using the constructor ?  Or for such case we cannot mock the call at all ?
reply
    Bookmark Topic Watch Topic
  • New Topic