Listing 6.4 First MainActivity test failed

Hello, I’m failed with List 6.4, help me please. Thank you very much.

  1. Source code
package com.bignerdranch.android.geoquiz

import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.*

import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MainActivityTest {
    private lateinit var scenario: ActivityScenario<MainActivity>

    @Before
    fun setUp() {
        scenario = launch(MainActivity::class.java)
    }

    @After
    fun tearDown() {
        scenario.close()
    }

    @Test
    fun showFirstQuestionOnLaunch() {
        onView(withId(R.id.question_text_view)).check(matches(withText(R.string.question_australia)))
    }
}
  1. Error message:
kotlin.UninitializedPropertyAccessException: lateinit property scenario has not been initialized
at com.bignerdranch.android.geoquiz.MainActivityTest.tearDown(MainActivityTest.kt:28)
  1. Disabled “Don’t keep activities” setting already.
  • failed on physical device (HONOR ROM-AN00), passed on virtual device (pixel 2 API 31);
  • run first MainActivity test and second MainActivity test together, the first one passed and the second on failed; run the second one only, passed.

run those three tests on virtual device (pixel 2 API 31) together, the first one passed only, and the error logs are:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'an instance of android.widget.TextView and view.getText() equals string from resource id: <2131689611>' doesn't match the selected view.
Expected: an instance of android.widget.TextView and view.getText() equals string from resource id: <2131689611> [question_oceans] value: The Pacific Ocean is larger than the Atlantic Ocean.
Got: view.getText() was "Canberra is the capital of Australia."
View Details: MaterialTextView{id=2131231055, res-name=question_text_view, visibility=VISIBLE, width=701, height=177, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@3ad86a4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=189.0, y=577.0, text=Canberra is the capital of Australia., input-type=0, ime-target=false, has-links=false}

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1724)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:12)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:5)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:12)
at com.bignerdranch.android.geoquiz.MainActivityTest.showsSecondQuestionAfterNextPress(MainActivityTest.kt:40)
... 29 trimmed
Caused by: junit.framework.AssertionFailedError: 'an instance of android.widget.TextView and view.getText() equals string from resource id: <2131689611>' doesn't match the selected view.
Expected: an instance of android.widget.TextView and view.getText() equals string from resource id: <2131689611> [question_oceans] value: The Pacific Ocean is larger than the Atlantic Ocean.
Got: view.getText() was "Canberra is the capital of Australia."
View Details: MaterialTextView{id=2131231055, res-name=question_text_view, visibility=VISIBLE, width=701, height=177, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@3ad86a4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=189.0, y=577.0, text=Canberra is the capital of Australia., input-type=0, ime-target=false, has-links=false}

at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:16)
at androidx.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:11)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:2)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:12)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)