4
私はAndroidアプリケーション用のUIテストを作成するのにEspressoを使用していますが、MockWebServerを使用してhttpリクエストを模擬したいと考えています。 テストを実行する前に、認証レスポンスを模擬してユーザーにサインインする必要があります。モックサーバーがAndroidエスプレッソのUIテストをリクエストします
アプリがmockwebserverを使って実際のリクエストを行う方法があるので、私はmockwebserverにエンキューされたレスポンスを使用できます。
は、これまでのところ私が持っている:ダガーとの依存関係を置き換えるの
public class AuthenticationTest {
@Rule
public ActivityTestRule<Authentication> mActivityTestRule = new ActivityTestRule<>(Authentication.class);
private Authentication activity;
private MockWebServer server;
@Before
public void signin() throws Exception {
server = new MockWebServer();
server.start();
activity = mActivityTestRule.getActivity();
MyApplication.State state = activity.getState();
String serverUrl = server.url("/").toString();
// Here is where I have a problem. How to force client to use mock server?
}
@Test
public void firstTest() {
String contentType = "Content-type: application/json";
MockResponse r1 = new MockResponse().setResponseCode(200).setBody("example_body").addHeader(contentType);
server.enqueue(r1);
// typing credentials and pressing "Sign in" button, which should use mocked server's response:
ViewInteraction email = onView(allOf(withId(R.id.emailAddress), isDisplayed()));
email.perform(replaceText("[email protected]"), closeSoftKeyboard());
ViewInteraction password = onView(allOf(withId(R.id.password), isDisplayed()));
password.perform(replaceText("some_password"), closeSoftKeyboard());
ViewInteraction signin = onView(allOf(withId(R.id.signInButton), withText("Sign In"), isDisplayed()));
button2.perform(click());
}