我是Android的Mockito测试的新手,并且在理解如何测试数据源方法时遇到了问题。我使用在应用程序的Application类中初始化的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连接。任何想法都会有所帮助。谢谢。
答案 0 :(得分:3)
看起来您的单元测试错误了:
您的方法是:
简单地说:这没有意义。我们的想法是将模拟对象传递给生产代码;然后你在生产代码上触发某种操作。最后,验证您希望看到的那些调用确实发生了。
除此之外:你没有测试库做了它应该做的事情(好吧,至少在单元测试中没有)。
相反,您的单元测试应该只测试您的生产所做的接线。换句话说:您唯一需要测试的是生产代码传递给某些库方法调用的参数是预期的参数。
当然,您应该进行一些“集成级别”测试,以确保整个工作端到端。但这通常超出单元测试的范围(正如名称已告诉我们的那样!)
答案 1 :(得分:1)
我认为错误可能出现在这一行:
verify(dataSource).saveCollection(collection, saveOrUpdateCollectionCallbackArgumentCaptor.capture());
更改:
verify(dataSource).saveCollection(collection, saveOrUpdateCollectionCallback);
您无法使用其他参数验证方法。