Here my Espresso's tests;

@RunWith(AndroidJUnit4::class)
class TradersActivityNetworkTest {
    private val context = InstrumentationRegistry.getInstrumentation().getContext()
    private lateinit var mockServer: MockWebServer


    @Rule
    @JvmField
    var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)
    //var tradersIntentTestRule = ActivityTestRule(TradersActivity::class.java)

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
    }

    @Test
    fun network_clientError_showToast() {
        tradersIntentTestRule.finishActivity()
        //stub response
        mockServer.enqueue(MockResponse()
                .setResponseCode(400))
        // start activity
        tradersIntentTestRule.launchActivity(Intent())

        onView(withText(R.string.client_error)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))
    }

    @Test
    fun network_serverError_showToast() {
        tradersIntentTestRule.finishActivity()
        mockServer.enqueue(MockResponse()
                .setResponseCode(500))
        // start activity
        tradersIntentTestRule.launchActivity(Intent())

        onView(withText(R.string.server_error)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))
    }

    @After
    @Throws
    fun tearDown() {
        // We're done with tests, shut it down
        mockServer.shutdown()
    }

}

Here custom matcher

class ToastMatcher : TypeSafeMatcher<Root>() {

    override fun describeTo(description: Description) {
        description.appendText("is toast")
    }

    override fun matchesSafely(root: Root): Boolean {
        val type = root.getWindowLayoutParams().get().type
        if (type == WindowManager.LayoutParams.TYPE_TOAST) {
            val windowToken = root.getDecorView().getWindowToken()
            val appToken = root.getDecorView().getApplicationWindowToken()
            if (windowToken === appToken) {
                return true
            }
        }
        return false
    }
}

Start only one test "network_clientError_showToast"

gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.myproject.activity.TradersActivityNetworkTest#network_clientError_showToast

Success pass. Nice.

Start another test (only this test) "network_serverError_showToast()" by

gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class==com.myproject.activity.TradersActivityNetworkTest#network_serverError_showToast

Success pass. Nice.

Now I want to start all tests by;

gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.myproject.activity.TradersActivityNetworkTest

thes test "network_serverError_showToast" is fail with error messaage:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131624000>[server_error] value: Server Error

View Hierarchy:
+>LinearLayout{id=-1, visibility=VISIBLE, width=422, height=149, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,192)(wrapxwrap) gr=#51 ty=2005 fl=#1000098 fmt=-3 wanim=0x1030004}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->AppCompatTextView{id=16908299, res-name=message, visibility=VISIBLE, width=272, height=83, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, [email protected], tag=null, root-is-layout-requested=false, has-input-connection=false, x=75.0, y=33.0, text=Client Error, input-type=0, ime-target=false, has-links=false}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:316)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at =com.myproject.activity.TradersActivityNetworkTest.network_serverError_showToast(TradersActivityNetworkTest.kt:88)
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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
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.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)

0 Answers