Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView Adapter is giving null on unit testing

I am trying to test RecyclerView with AndroidJunit4, it is my test code:

package com.kaushik.myredmart.ui;
// all includes
@RunWith(AndroidJUnit4.class)
public class ProductListActivityTest {

    @Rule
    public ActivityTestRule<ProductListActivity> rule  = new  ActivityTestRule<>(ProductListActivity.class);

    @Test
    public void ensureListViewIsPresent() throws Exception {
        ProductListActivity activity = rule.getActivity();
        View viewByIdw = activity.findViewById(R.id.productListView);
        assertThat(viewByIdw,notNullValue());
        assertThat(viewByIdw, instanceOf(RecyclerView.class));
        RecyclerView productRecyclerView = (RecyclerView) viewByIdw;
        RecyclerView.Adapter adapter = productRecyclerView.getAdapter();
        assertThat(adapter, instanceOf(ProductAdapter.class));

    }
}

I am facing a problem to check the Adapter. Although productRecyclerView is passing not null test and an instance of RecyclerView, it is following error in last line:

java.lang.AssertionError:
Expected: an instance of com.kaushik.myredmart.adapter.ProductAdapter
but: null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at com.kaushik.myredmart.ui.ProductListActivityTest.ensureListViewIsPresent(ProductListActivityTest.java:45)

What is the problem in code?

like image 655
dev_android Avatar asked May 02 '17 05:05

dev_android


People also ask

What does Adapter do to Recycler View?

Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView . A class that extends ViewHolder that will be used by the adapter.

When onCreateViewHolder is Called?

onCreateViewHolder is called when you need a new View. by Pavlos-Petros Tournaris | Medium. This is not the way a recycling works. onCreateViewHolder is called when you need a new View.

What is onCreateViewHolder?

onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders , and then automatically reuse them, each time calling onBindViewHolder .

What is RecyclerView Adapter in android?

RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they're needed. As the name implies, RecyclerView recycles those individual elements.


1 Answers

Judging from this line:

Expected: an instance of com.kaushik.myredmart.adapter.ProductAdapter but: null

One can conclude, that this:

RecyclerView.Adapter adapter = productRecyclerView.getAdapter();

returns null, which may happen when there has not been performed productRecyclerView.setAdapter(adapter).

Make sure you are correctly setting adapter in activity lifecycle callbacks (i.e. in onCreate()). It seems to me you are creating and setting adapter after some action/callback.

like image 133
azizbekian Avatar answered Sep 27 '22 18:09

azizbekian