错误模拟类,其中包含对SQLiteOpenHelper的引用

我为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. 我尝试StaticDatabaseHelper在setter方法中更改初始化,但这种方法无效!

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

任何想法?谢谢

回答:

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

./gradlew clean test

现在就可以了,StaticDatabaseHelper在这种情况下,我只是模拟类。这是我的最终测试 课程

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的引用 的全部内容, 来源链接: utcz.com/qa/414475.html

回到顶部