模拟Class的错误,该类保存对SQLiteOpenHelper

时间:2017-12-12 09:00:07

标签: java android unit-testing mockito

我为Presenter编写单元测试,需要模拟我的本地数据源

这是我的简单测试:

public class AddressPresenterTest {
    @Mock
    private AddressView mView;

    @Mock
    private AddressDataSource mDataSource;

    @Mock
    private AddressLocalDataSource mLocalDataSource;

    @Captor
    ArgumentCaptor<DataSourceCallback<Province>> mProvinceCallbackCaptor;

    private AddressPresenter mPresenter;

    @Before
    public void beforeTest() {
        MockitoAnnotations.initMocks(this);

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

    @Test
    public void When_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();
    }
}

当我运行此测试时,我收到了错误,

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class com.dai.uangteman.data.AddressLocalDataSource.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.77-b03
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0_77-b03
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 10.12.6

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

    at com.dai.uangteman.view.fragment.presenter.AddressPresenterTest.beforeTest(AddressPresenterTest.java:41)
...
Caused by: java.lang.IllegalArgumentException: Could not create type
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:140)
    ... 28 more
Caused by: java.lang.NoClassDefFoundError: android/database/sqlite/SQLiteOpenHelper
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
    ... 45 more
Caused by: java.lang.ClassNotFoundException: android.database.sqlite.SQLiteOpenHelper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    ... 87 more

当我解决方法改变我的AddressLocalDataSource课程时,我发现了一些奇怪的东西。

public class AddressLocalDataSource {
    private StaticDatabaseHelper mDatabaseHelper;

//    /**
//     * @param mDatabaseHelper
//     */
//    public AddressLocalDataSource(StaticDatabaseHelper mDatabaseHelper) {
//        this.mDatabaseHelper = mDatabaseHelper;
//    }

    public void setDatabaseHelper(StaticDatabaseHelper mDatabaseHelper) {
        this.mDatabaseHelper = mDatabaseHelper;
    }
    ...
}

低于我的发现:

  1. 我尝试删除包含StaticDatabaseHelper对象参数的构造函数和setter方法,它可以正常工作!
  2. 我尝试在setter方法中更改StaticDatabaseHelper初始化它不起作用!
  3. 问题是我的AddressLocalDataSource需要StaticDatabaseHelper类的实例。

    有什么想法吗?谢谢

3 个答案:

答案 0 :(得分:2)

似乎你还需要模拟StaticDatabaseHelper。然后Mockito应该自动将模拟的实例注入AddressLocalDataSource。因此,添加以下行可能会解决您的问题:

public class AddressPresenterTest {
    @Mock
    StaticDatabaseHelper mockedDatabaseHelper;
    ...

答案 1 :(得分:1)

  

✔回答

几个小时后,我寻找解决方案,我得到了最简单的解决方案。 我认为在android库中有一些不完整的测试。所以我只是运行这个gradle命令进行清理:

./gradlew clean test

它现在有效,我只是在这种情况下模拟StaticDatabaseHelper类。 所以这是我最后的测试 Class

public class AddressPresenterTest {

    @Mock
    private AddressView mView;

    @Mock
    private AddressDataSource mDataSource;

    @Mock
    private AddressLocalDataSource mLocalDataSource;

    @Captor
    ArgumentCaptor<DataSourceCallback<Province>> mProvinceCallbackCaptor;

    private AddressPresenter mPresenter;

    @Before
    public void beforeTest() throws Exception {
        MockitoAnnotations.initMocks(this);

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

    @Test
    public void When_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();
    }
}

希望这有帮助,谢谢

答案 2 :(得分:0)

就我而言,在我的项目中使用javassist-3.11.0.GA.jar。谷歌搜索之后,一些论文谈论了与javassist版本有关的问题,因此我尝试将javassist升级到javassist-3.18.2-GA.jar或javassist-3.21.0-GA.jar,此问题已解决。

其他一些问题可以通过以下方式解决:

  

java.lang.VerifyError:分支目标处的堆栈图框架不一致   40个例外

  

java.lang.RuntimeException:java.io.IOException:无效的常量   类型:18

  

基本异常:java.lang.IllegalArgumentException:无法   创建类型

此致