I'm testing a class TestObject. In this class there is a void returning function stageOne(String) which I want to test. At the end of stageOne there is a call to another void returning function, stageTwo(String). I want to fake the response from stageTwo so that stageTwo is never actually called. To pass the test TestObject needs to call stageTwo exactly once.
I've tried a variety of things using when...thenReturn but just can't get it to work. Below is my current best guess.
class TestClass {
private TestObject testObject;
@Before
public static void setUp() {
testObject = new TestObject();
}
@Test
public void test1() {
when(testObject.stageTwo(any(String.class))).thenReturn(Void);
testObject.stageOne("Test data");
verify(testObject, times(1)).stageTwo(any(String.class));
}
}
I'm using IntelliJ and have been able to do other tests, I just can't get the when...then constructs to work at all. At the moment with the above example IntelliJ is suggesting that I make it into a local variable OngoingStubbing<Void> voidOngoingStubbing and then it says that that's wrong too.
I have also tried the line
doNothing().when(testObject.stageTwo(any(String.class)));
but that doesn't work either. intelliJ underlines the contents of when() with the same message as before.
EDIT: TestObject structure below:
public class TestObject {
public void stageOne(String str) {
String str2 = ...
// do stuff
stageTwo(str2) // don't proceed with this method, but verify that it was called
}
public void stageTwo(String str) {
// do stuff which I don't want to do in this test
// e.g. calls to external services and other complexities
}
}