ArrayAdapter的Android JUnit测试抛出NullPointerException

时间:2016-06-06 12:13:06

标签: java android junit4

我正在尝试为我的项目测试ArrayAdapter,但它一直返回null。我不确定Mokito的模型中是否存在问题,但是带有模拟数据的ArrayList被传递给ArrayAdapter。附带测试的代码,有人可以重现错误并让我知道是否有解决方案在JUnit测试中将数据传递到ArrayAdapter?

import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import static org.junit.Assert.*;


//@RunWith(MockitoJUnitRunner.class)
public class QuickUnitTest extends AndroidTestCase
{

   private static ReadResultActivity activity;

   private static String TAG = "TAG";

   @Mock
   static Context mContext;

   private static CustomerAdapter customerTest;
   private static CustomerType mJohn = new CustomerType();
   private static CustomerType mJane = new CustomerType();

    public QuickUnitTest()
    {

    }

    @BeforeClass
    public void setUp()throws Exception
    {
        activity = mock(ReadResultActivity.class);

        ArrayList <CustomerType> data = new ArrayList<>();

        mJohn.setCustID(1);
        mJohn.setFirstName("John");
        mJohn.setLastName("Doe");
        mJohn.setGpsCoords("blah");

        mJane.setCustID(2);
        mJane.setFirstName("Jane");
        mJane.setLastName("Doe");
        mJane.setGpsCoords("");


        data.add(mJohn);
        data.add(mJane);
        Log.e(TAG,data.get(0).getFirstName());

        customerTest = new CustomerAdapter(getContext(), data); // error here? 
        //not passing arraylist
        //Log.d("customerTest",customerTest.getItem(0).getFirstName());

     }

     @Test
     public void testGetItem()
    {
        Log.d(TAG, mJohn.getFirstName());

        Log.e(TAG, customerTest.getItem(0).getFirstName());

        assertEquals("John is expected.", mJohn.getFirstName(),     customerTest.getItem(0).getFirstName());



    }

    @Test
    public void testGetItemId()
    {
        assertEquals("Wrong ID.", 0, customerTest.getItemId(0));
    }

    @Test
    public void testGetCount()
    {
        assertEquals("Contacts amount incorrect.", 2, customerTest.getCount());
    }


}

编辑:这是所要求的回溯

junit.framework.AssertionFailedError: Contacts amount incorrect. 
Expected :2
Actual   :0
<Click to see difference>


at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.Assert.failNotEquals(Assert.java:329)
at junit.framework.Assert.assertEquals(Assert.java:78)
at junit.framework.Assert.assertEquals(Assert.java:234)
at junit.framework.TestCase.assertEquals(TestCase.java:401)
at com.mylayouts.nk.assessment2_webservice.QuickUnitTest.testGetCount(QuickUnitTest.java:95)

java.lang.NullPointerException
at com.mylayouts.nk.webservice.QuickUnitTest.testGetItem(QuickUnitTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

0 个答案:

没有答案