本文介绍了错误模拟类,其中包含对SQLiteOpenHelper的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I write unit test for my Presenter, which is need to mock my Local Data Source.

这是我的简单测试:

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类时,我发现有些奇怪.

When i workaround to change my AddressLocalDataSource class i found something weird.

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初始化,但无效!
  1. I try to remove my constructor and setter method which is contain StaticDatabaseHelper object parameter and it works!
  2. I try to change the StaticDatabaseHelper initialization in setter method it doesn't work!

问题是我的AddressLocalDataSource需要StaticDatabaseHelper类的实例.

The problem is my AddressLocalDataSource need instance of StaticDatabaseHelper class.

有什么主意吗?谢谢

推荐答案

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

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

它现在可以工作了,在这种情况下,我只是模拟了StaticDatabaseHelper类.这是我的最终测试 Class :

And it works now, i just mock StaticDatabaseHelper class in this case.So this is my final testing 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();
    }
}

希望获得帮助,谢谢

这篇关于错误模拟类,其中包含对SQLiteOpenHelper的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:44