samedi 25 juin 2016

mocking public static inner class

I'm using GreenRobot DAO as a DB ORM and I need to mock out a call tonew DaoMaster.DevOpenHelper(...). The issue is that I can't seem to get Powermockito/mockito to return the mocked object when using whenNew.

The class that I am trying to mock has the following basic structure:

public class DaoMaster{
    public static class DevOpenHelper{...}
}

my test case looks like:

    @Test
    @PrepareOnlyThisForTest({ClassUsingDevOpenHelper.class})
    public void testAsyncTask()throws Exception{
        // mocks
        DaoMaster.DevOpenHelper helperMock = PowerMockito.spy(
                new DaoMaster.DevOpenHelper(
                        getActivity(),
                        FileEntry.DB_NAME,
                        null));

        // when
        PowerMockito.whenNew(DaoMaster.DevOpenHelper.class).withAnyArguments().thenReturn(helperMock);
    }

I then proceed to get a null pointer exception inside of ClassUsingDevOpenHelper where I actually use the returned object. It also returns null with other classes. I've also tried to add DaoMaster.class to @ PrepareOnlyThisForTest with no success.

Aucun commentaire:

Enregistrer un commentaire