浓缩咖啡:依次运行多个测试会导致ListView适配器上出现IllegalStateException

时间:2018-09-01 02:26:54

标签: java android android-espresso

我一直在尝试运行以下测试,似乎一次运行一个测试似乎运行良好,但是当我尝试一次运行所有这些时,我会收到此错误

  

适配器的内容已更改,但ListView没有收到   通知。确保适配器的内容未修改   来自后台线程,但仅来自UI线程。确保你的   适配器的内容更改时,适配器将调用notifyDataSetChanged()。

我的测试类包含两个测试函数。

import android.support.test.espresso.Espresso;
import android.support.test.espresso.ViewInteraction;
import android.support.test.espresso.action.ViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;


import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.*;
import static android.support.test.espresso.matcher.ViewMatchers.*;
import static android.support.test.espresso.action.ViewActions.*;
import static android.support.test.espresso.assertion.ViewAssertions.*;
import static org.hamcrest.Matchers.*;

import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class)
public class NoteListActivityTest {

    static DataManager sDataManager;
    @BeforeClass
    public static void initDataManager(){
        sDataManager = DataManager.getInstance();
    }
    @Before
    public void setUp(){

        sDataManager.getNotes().clear();
        sDataManager.initializeExampleNotes();
    }
    @Rule
    public ActivityTestRule<NoteListActivity> mNoteListActivity = new ActivityTestRule<>(NoteListActivity.class);

    @Test
    public void createNote(){
        CourseInfo course = sDataManager.getCourse("android_intents");
        String noteTitle = "Working with intents";
        String noteText = "Intents are cool as we can open an activity through an intent";



        onView(withId(R.id.fab)).perform(click());
        onView(withId(R.id.spinner_courses)).perform(click());
        onData(allOf(instanceOf(CourseInfo.class),equalTo(course))).perform(click());
        onView(withId(R.id.spinner_courses))
                .check(matches(withSpinnerText(course.getTitle())));
        onView(withId(R.id.text_note_title)).perform(typeText(noteTitle), ViewActions.closeSoftKeyboard())
                .check(matches(withText(containsString(noteTitle))));
        onView(withId(R.id.text_note_text)).perform(typeText(noteText),ViewActions.closeSoftKeyboard());

        //Check an Assertion
        onView(withId(R.id.text_note_text)).check(matches(withText(containsString(noteText))));
        Espresso.pressBack();

        int index = sDataManager.getNotes().size()-1;
        NoteInfo note = sDataManager.getNotes().get(index);
        assertEquals(course,note.getCourse());
        assertEquals(noteTitle,note.getTitle());
        assertEquals(noteText,note.getText());

    }
    @Test
    public void checkCreatedNoteinList(){
        CourseInfo course = sDataManager.getCourse("android_async");
        String noteTitle = "Working with async tasks";
        String noteText = "Async tasks are cool as we can handle long running tasks in the background at ease.";



        onView(withId(R.id.fab)).perform(click());
        onView(withId(R.id.spinner_courses)).perform(click());
        onData(allOf(instanceOf(CourseInfo.class),equalTo(course))).perform(click());
        onView(withId(R.id.text_note_title)).perform(typeText(noteTitle), ViewActions.closeSoftKeyboard());
        onView(withId(R.id.text_note_text)).perform(typeText(noteText),ViewActions.closeSoftKeyboard());

        Espresso.pressBack();
        int index = sDataManager.getNotes().size()-1;
        NoteInfo note = sDataManager.getNotes().get(index);
        onData(allOf(instanceOf(NoteInfo.class),equalTo(note))).perform(scrollTo(),click());
        onView(withId(R.id.spinner_courses))
                .check(matches(withSpinnerText(course.getTitle())));
        onView(withId(R.id.text_note_title))
                .check(matches(withText(containsString(noteTitle))));
        onView(withId(R.id.text_note_text))
                .check(matches(withText(containsString(noteText))));
    }
}

同时运行它们会产生此错误:

android.support.test.espresso.PerformException: Error performing 'single click - At Coordinates: 947, 1643 and precision: 16, 16' on view 'with id: com.example.himanshu.noteapp:id/fab'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:79)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:51)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:173)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:114)
at com.example.himanshu.noteapp.NoteListActivityTest.checkCreatedNoteinList(NoteListActivityTest.java:75)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.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.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java: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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
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.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2022)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at android.support.test.espresso.base.Interrogator.getNextMessage(Interrogator.java:205)
at android.support.test.espresso.base.Interrogator.loopAndInterrogate(Interrogator.java:140)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:462)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:421)
at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:235)
at android.support.test.espresso.action.MotionEvents.sendDown(MotionEvents.java:83)
at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:168)
at android.support.test.espresso.action.Tap.access$100(Tap.java:31)
at android.support.test.espresso.action.Tap$1.sendTap(Tap.java:47)
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:136)
at android.support.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:356)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:248)
at android.support.test.espresso.ViewInteraction.access$100(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.test.espresso.base.Interrogator.getNextMessage(Interrogator.java:199)
... 22 more
Caused by: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230817, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1618)
at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:3756)
at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:997)
at android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3752)
at android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:3736)
at android.view.ViewRootImpl$EarlyPostImeInputStage.processPointerEvent(ViewRootImpl.java:4252)
at android.view.ViewRootImpl$EarlyPostImeInputStage.onProcess(ViewRootImpl.java:4220)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3861)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6257)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6196)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6157)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6360)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
... 24 more

0 个答案:

没有答案