Skip to content Skip to sidebar Skip to footer

Error Mocking Class Which Hold Reference To Sqliteopenhelper

I write unit test for my Presenter, which is need to mock my Local Data Source. Here is my simple test : public class AddressPresenterTest { @Mock private AddressView mVie

Solution 1:

Seems you also need to mock StaticDatabaseHelper. Then Mockito should automatically inject your mocked instance into the AddressLocalDataSource. So adding following line may solves your issue:

publicclassAddressPresenterTest {
    @Mock
    StaticDatabaseHelper mockedDatabaseHelper;
    ...

Solution 2:

✔ Answer

After a couple hour i looking for solution, i get the simplest solution. I think there is some incomplete in android library for testing. So i just run this gradle command to cleanup :

./gradlew clean test

And it works now, i just mock StaticDatabaseHelper class in this case. So this is my final testing Class :

publicclassAddressPresenterTest {

    @Mockprivate AddressView mView;

    @Mockprivate AddressDataSource mDataSource;

    @Mockprivate AddressLocalDataSource mLocalDataSource;

    @Captor
    ArgumentCaptor<DataSourceCallback<Province>> mProvinceCallbackCaptor;

    private AddressPresenter mPresenter;

    @BeforepublicvoidbeforeTest()throws Exception {
        MockitoAnnotations.initMocks(this);

        mPresenter = newAddressPresenter(mDataSource, mView);
        mPresenter.setLocalDataSource(mLocalDataSource);
    }

    @TestpublicvoidWhen_SelectProvince_DataIsNull_ShowErrorMessage() {
        mPresenter.getLocalProvinceById(2129023);

        // Cause data source has callback, we need to capture the callback
        ArgumentCaptor<Integer> provinceIdCaptor = ArgumentCaptor.forClass(Integer.class);
        verify(mLocalDataSource).fetchProvinceById(provinceIdCaptor.capture(), mProvinceCallbackCaptor.capture());
        mProvinceCallbackCaptor.getValue().onFailedLoad();

        verify(mView).loadContentError();
    }
}

Hope this help, thank you

Solution 3:

In my case, in my project using javassist-3.11.0.GA.jar. After google some papers talk about the issue above related to javassist verison so I try to upgrade javassist to javassist-3.18.2-GA.jar or javassist-3.21.0-GA.jar the issue is resolved.

Some others issues can resolve by this way as below:

java.lang.VerifyError: Inconsistent stackmap frames at branch target 40 Exception

,

java.lang.RuntimeException: java.io.IOException: invalid constant type: 18

,

Underlying exception : java.lang.IllegalArgumentException: Could not create type

Regards,

Post a Comment for "Error Mocking Class Which Hold Reference To Sqliteopenhelper"