我是Android中Mockito测试的新手,在理解如何测试数据源方法方面有问题。我使用在应用程序的应用程序类中初始化的RushOrm:
AndroidInitializeConfig config = new AndroidInitializeConfig(getApplicationContext());
List<Class<? extends Rush>> classes = new ArrayList<>();
classes.add(CardCollection.class);
classes.add(Note.class);
config.setClasses(classes);
RushCore.initialize(config);
我的数据源类是:
public class CollectionsRepository implements CollectionDataSource {
private static CollectionsRepository INSTANCE = null;
private final CollectionDataSource dataSource;
// Prevent direct instantiation.
private CollectionsRepository(@NonNull CollectionDataSource dataSource) {
this.dataSource = checkNotNull(dataSource);
}
public static CollectionsRepository getInstance(CollectionDataSource dataSource) {
if (INSTANCE == null) {
INSTANCE = new CollectionsRepository(dataSource);
}
return INSTANCE;
}
@Override
public void getCollections(@NonNull LoadCollectionsCallback callback) {
dataSource.getCollections(callback);
}
@Override
public void getCollection(@NonNull String collectionId, @NonNull GetCollectionCallback callback) {
dataSource.getCollection(collectionId, callback);
}
@Override
public void saveCollection(@NonNull CardCollection cardCollection, @NonNull final SaveOrUpdateCollectionCallback callback) {
dataSource.saveCollection(cardCollection, callback);
}
@Override
public void updateCollection(@NonNull CardCollection cardCollection, @NonNull SaveOrUpdateCollectionCallback callback) {
dataSource.updateCollection(cardCollection, callback);
}
@Override
public void deleteCollection(@NonNull CardCollection cardCollection) {
dataSource.deleteCollection(cardCollection);
}
@Override
public void deleteAllCollections(@NonNull RushCallback callback) {
dataSource.deleteAllCollections(callback);
}
}
这是我的Repository类的测试类:
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP, application = ProjectApplication.class)
@RunWith(MockitoJUnitRunner.class)
public class CollectionsRepositoryTest {
private CollectionPresenter presenter;
@Mock
private CollectionsRepository repository;
@Mock
private CollectionContract.View view;
@Mock
private CollectionDataSourceImplementation dataSource;
@Mock
private CollectionDataSource.GetCollectionCallback getCollectionCallback;
@Mock
private CollectionDataSource.LoadCollectionsCallback loadCollectionsCallback;
@Mock
private CollectionDataSource.SaveOrUpdateCollectionCallback saveOrUpdateCollectionCallback;
@Captor
private ArgumentCaptor<CollectionDataSource.GetCollectionCallback> getCollectionCallbackArgumentCaptor;
@Captor
private ArgumentCaptor<CollectionDataSource.LoadCollectionsCallback> loadCollectionsCallbackArgumentCaptor;
@Captor
private ArgumentCaptor<CollectionDataSource.SaveOrUpdateCollectionCallback> saveOrUpdateCollectionCallbackArgumentCaptor;
private static List<CardCollection> collections = Lists.newArrayList(new CardCollection("Title1", "Description1", null),
new CardCollection("Title2", "Description2", null));
private final static String collection_title = "title";
@Before
public void setUp() {
// Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
// inject the mocks in the test the initMocks method needs to be called.
MockitoAnnotations.initMocks(this);
// Get a reference to the class under test
presenter = new CollectionPresenter(repository, view);
}
@Test
public void getCollections() {
dataSource.getCollections(loadCollectionsCallback);
verify(dataSource).getCollections(loadCollectionsCallbackArgumentCaptor.capture());
loadCollectionsCallbackArgumentCaptor.getValue().onSuccess(collections);
}
@Test
public void getCollection() {
dataSource.getCollection(collection_title, getCollectionCallback);
verify(dataSource).getCollection(eq(collection_title), any(CollectionDataSourceImplementation.GetCollectionCallback.class));
}
@Test
public void saveCollection() {
CardCollection collection = new CardCollection("Title", "Description", null);
dataSource.saveCollection(collection, saveOrUpdateCollectionCallback);
verify(dataSource).saveCollection(collection, saveOrUpdateCollectionCallbackArgumentCaptor.capture());
saveOrUpdateCollectionCallbackArgumentCaptor.getValue().onSuccess(collection);
}
@Test
public void updateCollection() {
}
@Test
public void deleteCollection() {
}
@Test
public void deleteAllCollections() {
}
}
saveCollection方法失败。我在谷歌上搜索,主要是想知道如何测试直接的sqlite连接。任何想法都会有所帮助。非常感谢。
看起来单元测试出错了:
你的方法是:
- 测试方法调用mocke对象上的方法
- 然后"验证"是否调用了模拟方法
简单地说:那没有意义。其想法是将模拟对象传递到生产代码;然后对生产代码触发某种操作。最后,你验证那些你期望看到的电话真的发生了。
除此之外:您不会测试库是否做了它应该做的事情(好吧,至少在单元测试中不会)。
相反,您的单元测试应该只测试生产所做的接线。换句话说:您唯一需要测试的是,生产代码传递给某个库方法调用的参数是预期的参数。
当然,您应该进行一些"集成级别"的测试,以确保整个工作端到端。但这通常是超出单元测试的范围(正如名称已经告诉我们的那样!)
我认为错误可能在这一行:
verify(dataSource).saveCollection(collection, saveOrUpdateCollectionCallbackArgumentCaptor.capture());
更改依据:
verify(dataSource).saveCollection(collection, saveOrUpdateCollectionCallback);
无法使用其他参数验证方法。