Skip to content
Advertisement

how to stub a method in a class which is called from another class

JavaScript

I’m not able to get my method to return the desired value. Above implementations shows how I have written my test, but it’s not working.

What am i doing wrong here?

Advertisement

Answer

Yet to clarify given answers: your mistake is that you do not actually use that spy/mock anywhere.

In your class A you have a static method where a new B is always instantiated instead of using your spy:

JavaScript

Spy does not make class somehow globally spied but creates just one instance that is spied and can be mocked. And yet again: this new B() is not a spy nor mock.

One possible working solution would be to change your design a bit (as already suggested) and let Mockito inject this spied B in an instance of A:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement