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?
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.
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.
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.
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 ?
Just the other day, I was thinking ... about this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss