I have a test where I have written most the unit tests I need, but there is one I want to test the first bits of the logic. I don't care what happens in the rest of the method as long as the method has been called, but since the SUT fails with an NPE due to lack of further mocking, the test fails before I get to my assertion.
Is there a clean way of asserting a method was called and ignore all exceptions happening after that? Perhaps some kind of blocking behavior on hitting the method and aborting/passing the test after that?
I was thinking I could write when(mock.methodIAmInterestedIn(any)).thenThrow(new RuntimeException("verified!"))
and then simply assert that I get the right exception (it will be wrapped inside another, though). This would probably work, but it is a bit unclean: obscuring what I really want to test.
@Test
public void should_load_using_filename_in_config() {
loader = new OrgUnitLoader(config, dbSupport.mockServices);
config = mock(TestConfiguration.class);
/* further mocking of the config needed if I am not to get an NPE */
when(dao.orgUnitsAreLoaded()).thenReturn(false);
// call the actual method
loader.loadOrgUnits();
verify(config, times(1)).getString(ORG_UNIT_DATA_FILE);
}
Could a CountdownLatch
be used, for instance?