mercredi 2 mai 2018

NPE when mocking Annotation method

The task is to cause NoSuchMethodException exception when calling table.annotationType().getMethod("name").

I have following code

@Mock(answer = RETURNS_DEEP_STUBS)
private Table table;

@Test(expectedExceptions = NoSuchMethodException.class)
public void testGetSupportClassesDatabaseThrowException() throws NoSuchMethodException {
    String testPackageName = "com.usc.dbd.util.supporttable";

    MockitoAnnotations.initMocks(this);

    when(table.annotationType().getMethod("name")).thenThrow(NoSuchMethodException.class);

    SupportTables.getSupportClasses(testPackageName);
}

The test fails with NullPointerException on when... line.

Probably the issue is in mocking Table interface.

I'm looking for solution how to test such case.





Aucun commentaire:

Enregistrer un commentaire