我正在寻求从LoginActivy创建一个器乐(espresso)测试。 我正在使用Mock进行API调用,不幸的是我的应用程序一直在调用我的REAL API。
以下是我的测试代码
@Test
public void loginSuccessTest() throws Exception {
String fileName = "login/success_login_with_user_pass.json";
try {
Thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withId(R.id.txt_username)).perform(replaceText(USER_VALID));
onView(withId(R.id.txt_password)).perform(replaceText(PASS_VALID), closeSoftKeyboard());
server.enqueue(
new MockResponse()
.setResponseCode(200)
.setBody(RestServiceTestHelper.getStringFromFile(getInstrumentation().getContext(), fileName)));
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.btn_login), withText("Acessar")));
appCompatButton.perform(scrollTo(), click());
onView(withId(R.id.btn_main_menu)).check(matches(isDisplayed()));
}
当测试通过下面的代码时,测试会调用REAL API。
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.btn_login), withText("Acessar")));
appCompatButton.perform(scrollTo(), click());
这是测试开始前的代码
@Before
public void setUp() throws Exception {
server = new MockWebServer();
server.start(8681);
}
应用程序正在使用productFlavors配置:
productFlavors {
ui_tests {
buildConfigField STRING, 'HOST', '"http://localhost:8681/"'
}
}
我不知道还有什么可以避免我的REAL API中的调用,有人可以帮助我吗?