Android test recyclerview adapter
I have a basic recyclerviewadapter. I want to test independently of my subclasses. How can I isolate it? I try to create a concise vanilla activity in the test class and use activitytestrule to start it, but unfortunately, the test framework seems to want to start activities in the actual application rather than test activities in the application. I don't want to use robolectric for this, Because our team is committed to instrument testing with espresso. What I really want to test is the behavior of various notify methods in the adapter, because I see a crash
resolvent:
I finally solve this problem by adding virtual activities in the debug folder, and then manually add recyclerview to the activity in the test code, and then set the adapter to isolate the test on it. When the application is compiled, the manifest merge will merge all the activities declared in androidmanifest.xml in the debug folder
This is my test setup code:
@RunWith(AndroidJUnit4.class)
public class MyRecyclerViewAdapterTest {
private MyRecyclerViewAdapter adapter;
private RecyclerView recyclerView;
@Rule
public ActivityTestRule<DummyActivity> activityTestRule =
new ActivityTestRule<>(DummyActivity.class, true, false);
@Rule
public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
@Before
public void setup() throws Throwable {
final DummyActivity activity = activityTestRule.launchActivity(null);
uiThreadTestRule.runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new MyRecyclerViewAdapter(activity);
recyclerView = new RecyclerView(activity);
recyclerView.setId(R.id.recycler_view);
activity.setContentView(recyclerView);
recyclerView.setLayoutManager(new linearlayoutmanager(activity));
recyclerView.setAdapter(adapter);
}
});
}
}
The virtual activity is declared in / SRC / debug / androidmanifest.xml:
<activity android:name="com.example.DummyActivity" />
Virtual activities are simple:
// Dummy Activity for testing
public class DummyActivity extends Activity {
}