I'm testing a void method that happens to call several other void methods in a class (all of these methods are in the same class). The method is something along these lines...
public void methodToTest() {
methodA();
methodB();
}
void methodA() {
methodA1();
methodA2();
methodA3();
}
What I'd like to do is cause methodA()
above to do nothing. That is, I want methodA()
to basically be like this:
void methodA() { }
I've tried both doThrow()
and doAnswer()
on methodA()
to no avail. It's as if those are both being completely ignored.
An example of what I've tried...
doThrow(new RuntimeException()).when(mockedClass).methodA();
Is there a way to do this just using Mockito? I'm not at liberty to change the class that's being modified.
methodA
is notfinal
?methodA
is notfinal
methodB
, while you are executingmethodA
. You are most certainly usingmock(YourObject.class)
, then of course a mock does nothing, and certainly will never execute real code. You can achieve what you want aspy(YourObject.class)
it will execute the real code by default. However partial mocks is recognized as test smell, if not a design smell, you should look for opportunities to make the behavior ofmethodA
as a dependency that you could expose in your API as aAStrategy
for example.methodA
has no side effect, mock the objects thatmethodA
calls to achieve the desired result.