我正在寻找从 LoginActivy 创建一个工具(浓缩咖啡)测试。我正在使用 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 中调用,有人可以帮助我吗?
