package com.sqisland.mockwebserver_demo;

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;

import okhttp3.mockwebserver.MockResponse;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

public class MainActivityTest {
  @Rule
  public ActivityTestRule<MainActivity> activityRule
      = new ActivityTestRule<>(MainActivity.class, true, false);

  @Rule
  public OkHttpIdlingResourceRule okHttpIdlingResourceRule = new OkHttpIdlingResourceRule();

  @Rule
  public MockWebServerRule mockWebServerRule = new MockWebServerRule();

  @Before
  public void setBaseUrl() {
    TestDemoApplication app = (TestDemoApplication)
        InstrumentationRegistry.getTargetContext().getApplicationContext();
    app.setBaseUrl(mockWebServerRule.server.url("/").toString());
  }

  @Test
  public void followers() throws IOException {
    mockWebServerRule.server.enqueue(new MockResponse().setBody(
        "{ \"login\" : \"octocat\", \"followers\" : 1500 }"));

    activityRule.launchActivity(null);

    onView(withId(R.id.followers))
        .check(matches(withText("octocat: 1500")));
  }
}
@@ -1467,10 +1467,267 @@
 )));%0A  %7D
+%0A%0A  @Test%0A  public void status404() throws IOException %7B%0A    mockWebServerRule.server.enqueue(new MockResponse().setResponseCode(404));%0A%0A    activityRule.launchActivity(null);%0A%0A    onView(withId(R.id.followers))%0A        .check(matches(withText(%22404%22)));%0A  %7D
 %0A%7D

Test 404 status