More complete question is, given a dependency that expects a callback as a parameter, how do I write a unit test that covers the callback logic and still manage to mock up the dependency?
public class DoStuff {
public void runThis(Runnable callback) {
// call callback
}
}
public class ClassUnderTest {
private DoStuff stuffToDo;
public void methodUnderTest() {
this.stuffToDo.runThis(/*a runnable with some logic*/)
}
}
In the example above, I would mock stuffToDo
since I should verify calls and mock outputs of method calls. However, mocking runThis
results in the callback logic not being tested. Furthermore, callback logic seems like it should be private so I wouldn't expect to test it directly; perhaps that is a misconception on my part.
Since callbacks are used rather extensivly, I would expect there to be a common method for testing them but I haven't found it.