dimanche 1 novembre 2015

Test case to verify invocation even after one of the dependency call throws exception

In my method, I invoke two dependencies depA.foo() & depB.bar(). If depA throws exception, then I don't want depB to be invoked. (NOT vice versa). So in the codebase, call to depA should be before call to B.

Is there a way to write test case to test invocation of depB() without explicity catching exception thrown by invocation A?

I could do this by putting verify() in finally block, but not sure if this is the correct way. Please let me know if there is any other standard way to do this

//Sample code
public void someMethod(){
depA.foo();
depB.bar();
}
//Sample Test
@Test(expectedExceptions = {RuntimeException.class})
public void someTest(){
when(depA.foo()).thenThrow(new RuntimeException("Some method"));
try{
 toTestClass.someMethod();
} finally {
 verify(depB,times(0)).bar();
}
}

Aucun commentaire:

Enregistrer un commentaire