vendredi 27 mai 2016

Mocking an Activity with a RecyclerView in Espresso error

I am developping an Android project by TDD with Espresso/JUnit and Android Studio. The activity I want to test, and for which I made a Mock object, has a RecyclerView as a child. But it seems, that when mocking my activity, the test framework could not instantiate the ArrayAdapter of my RecyclerView.

This is test error stacktrace :

java.lang.AssertionError: LayoutInflater not found.
at android.view.LayoutInflater.from(LayoutInflater.java:231)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:178)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:163)
at  com.loloof64.chess_positions_archiver.file_explorer_activity.DiskStorageLi stAdapter.<init>(DiskStorageListAdapter.java:33)
at    com.loloof64.chess_positions_archiver.file_explorer_activity.DiskStorageLi stAdapterTest.when_weInitializeAdapter_then_itsContentListIsEmpty(DiskStor ageListAdapterTest.java:36)
at java.lang.reflect.Method.invoke(Native Method)
at  org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMetho d.java:50)
at  org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable .java:12)
at  org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod. java:47)
at  org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.j ava:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at  org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.j ava:78)
at  org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.j ava:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at  android.support.test.internal.runner.TestExecutor.execute(TestExecutor.jav a:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)

this is the DiskStorageListAdapterTest.java :

package com.loloof64.chess_positions_archiver.file_explorer_activity;

import com.loloof64.chess_positions_archiver.FileExplorerActivity;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class DiskStorageListAdapterTest {

    @Mock private static FileExplorerActivity explorerActivity;

    @Test
    public void when_weInitializeAdapter_then_itsContentListIsEmpty(){
        DiskStorageListAdapter storageListAdapter = new DiskStorageListAdapter(explorerActivity);
        DiskStorage [] expectedList = new DiskStorage[0];
        assertArrayEquals("New DiskStorageListAdapter should be empty", expectedList, storageListAdapter.toArray());
    }

}

This is the FileExplorerActivity.java :

package com.loloof64.chess_positions_archiver;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FileExplorerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_explorer);
    }

}

This is the activity_file_explorer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"                      
    tools:context="com.loloof64.chess_positions_archiver.FileExplorerActivity"    >

    <ListView
        android:id="@+id/file_explorer_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

This is DiskStorageListAdapter.java:

package com.loloof64.chess_positions_archiver.file_explorer_activity;

import android.content.Context;
import android.widget.ArrayAdapter;

import com.loloof64.chess_positions_archiver.R;

import java.util.ArrayList;

/**
 * An adapter between RecyclerView component and a list of DiskStorage    instances.
 */
public class DiskStorageListAdapter extends ArrayAdapter<DiskStorage>     {

    public DiskStorageListAdapter(Context context){
        super(context, R.layout.disk_storage_adapter_item_layout, new  ArrayList<DiskStorage>());
    }

    public DiskStorage[] toArray() {
        return null;
    }
}

This is disk_storage_adapter_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SampleText"/>

</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire