How start and not start activity in differents tests in one class?

0

My application first load list of items by http request (Retrofit) and then show it in RecyclerView .

Nice.

Now I write many Espresso's test to check screen(activity = TradersActivity) that show result of loaded list.

So I mock load items by okhttp3.mockwebserver.MockResponse and put them to @Before method. As result first load data and then start espresso test (e.g. itemList_isDisplayed()) Here snippet of Espresso's tests:

i

mport okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer

@RunWith(AndroidJUnit4::class)
class TradersActivityTest {

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

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

        mockServer.enqueue(MockResponse()
                .setResponseCode(200)
                .setBody(FileUtil.getStringFromFile(context, DEFAULT_TRADERS_LIST)));
        tradersIntentTestRule.launchActivity(Intent())
    }

   @Test
    fun toolBar_height() {
        onView(withId(R.id.toolBar))
                .check(matches(withHeightResId(R.dimen.tool_bar_height)))
    }

    @Test
    fun itemList_isDisplayed() {
        onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
        onView(withRecyclerView(R.id.tradersRecyclerView).atPosition(checkItemCount))
                .check(matches(isDisplayed()))
    }

    @Test
    fun itemList_BaseisDisplayed() {
        onView(withId(R.id.tradersRecyclerView))
                .perform(scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
        onView(withRecyclerView(R.id.tradersRecyclerView).atPositionOnView(checkItemCount, R.id.baseTextView))
                .check(matches(isDisplayed()))
    }

}

Nice. it's work fine.

But now I want to write test that check is show toast when has http response return error (e.g. http status = 400). And I add this test:

 @Test
    fun network_clientError_showToast() {
        mockServer.enqueue(MockResponse()
                .setResponseCode(400))
        tradersIntentTestRule.launchActivity(Intent())
        onView(withText(R.string.client_error)).inRoot(ToastMatcher()).check(matches(isDisplayed()))
}

As you can see this test "network_clientError_showToast" first stub error response and then start activity. As result when I start thie test I get error:

java.lang.RuntimeException: Could not launch intent Intent { flg=0x10000000 cmp=com.myproject.debug/com.myproject.ui.activity.TradersActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1556450658437 and now the last time the queue went idle was: 1556450667375. If these numbers are the same your activity might be hogging the event queue.
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:459)
at androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:358)
at 

So I need two things:

  1. Start TradersActivity on @Before method. As result I'm not need to start activity on EVERY test that check UI.

  2. Start TradersActivity on tests that check network errors (like http status = 400)

How I can do this in one class TradersActivityTest ? Thanks.

android-espresso
ui-testing
mockserver
asked on Stack Overflow Apr 28, 2019 by Alex

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0