我正在尝试使用 AndroidJunit4 测试 RecyclerView,这是我的测试代码:
@Rule
public ActivityTestRule<ProductListActivity> rule = new ActivityTestRule<>(ProductListActivity.class);
............................
..........................
@Test
public void ensureDataIsLoadingOnSuccess() throws Exception {
ProductListActivity activity = rule.getActivity();
...........................
............
activity.runOnUiThread(new Runnable() {
public void run() {
activity.displayProducts(asList(product1, product2), 0);
}
});
assertEquals(2, mAdapter.getItemCount());
assertThat(((ProductAdapter) mAdapter).getItemAtPosition(0),sameInstance(product1));
assertThat(((ProductAdapter) mAdapter).getItemAtPosition(1),sameInstance(product2));
}
这是我在 Activity 中的 displayProducts() 代码:
@Override
public void displayProducts(List<Product> products, Integer pageNo) {
progressBar.setVisibility(View.GONE);
if (pageNo == 0 && products.size() == 0) {
noProductTextView.setVisibility(View.VISIBLE);
} else {
mProductAdapter.addProduct(products);
noProductTextView.setVisibility(View.GONE);
productListView.setVisibility(View.VISIBLE);
}
}
它给出的错误如下:
junit.framework.AssertionFailedError: expected:<2> but was:<0>
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.Assert.failNotEquals(Assert.java:287)
at junit.framework.Assert.assertEquals(Assert.java:67)
at junit.framework.Assert.assertEquals(Assert.java:199)
at junit.framework.Assert.assertEquals(Assert.java:205)
at com.kaushik.myredmart.ui.ProductListActivityTest.ensureDataIsLoadingOnSuccess(ProductListActivityTest.java:94)
请帮助我的代码有什么问题?