我是新來的Mockito在Android測試,並有一個理解如何測試數據源方法的問題。我使用的是在應用程序類應用程序的初始化RushOrm:如何在Android中使用Mockito正確測試ORM?
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);
我的數據源類是這樣的:
這是我的庫類測試類:
@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連接。任何想法都會有所幫助。謝謝。
嗨,測試的目標是什麼?你在哪一點嘲笑。我對Mockito知之甚少,但可以在RushOrm的工作中幫助你。 –
嗨,我想從應用程序測試與RushOrm的操作正常工作。 – user1140656
如果你想測試真正的庫,你並不想嘲笑任何東西。你可以使用標準的android測試框架來測試它。你可能想嘲笑你自己的課程,但我不認爲你想嘲笑數據源的東西,因爲那時你正在嘲笑你真正想測試的東西。RushOrm有一個測試負載檢查出來可能會幫助你開始https://github.com/Stuart-campbell/RushOrm/tree/master/RushORM/rushandroid/src/androidTest/java/co/uk/rushorm/android –