我正在使用Espresso框架编写UI测试,我需要模拟服务器响应,因此我使用Mockwebserver。 但是执行onView(withId(R.id.fragment_login_btn_enter))。perform(click());之后不执行进一步检查(由调试器检查)。尽管Web服务器响应并且应用程序正常运行。有什么问题吗?
这是测试和调度程序的代码:
LoginUINetworkTest.java
@RunWith(AndroidJUnit4.class)
public class LoginUINetworkTest extends ActivityWithMockingNetworkUITest<LoginScreen> {
@Override
protected Class<LoginScreen> provideActivityClass() {
return LoginScreen.class;
}
@Override
protected Dispatcher provideDispatcher(String testName) {
return new SuccessAuthorizeDispatcher(InstrumentationRegistry.getInstrumentation().getTargetContext());
}
@Test
public void uiNetSuccessLogin() {
LoginScreen activity = activityRule.getActivity();
FragmentManager supportFragmentManager = activity.getSupportFragmentManager();
DialogFragmentIdlingResource dialogFragmentIdlingResource = new DialogFragmentIdlingResource(
LoginScreen.BOTTOM_LOGIN_TAG,
supportFragmentManager
);
onView(withId(R.id.activity_login_btn_login)).perform(click());
idlingBlock(dialogFragmentIdlingResource, () -> {
onView(withId(R.id.fragment_login_et_passphrase))
.check(matches(isDisplayed()));
onView(withId(R.id.fragment_login_et_passphrase))
.perform(typeText(InstrumentedTestConstants.PASSPHRASE))
.perform(closeSoftKeyboard());
onView(withId(R.id.fragment_login_btn_enter)).perform(click());
});
String mainActivity = MainScreen.class.getName();
ActivityIdlingResosurce activityIdlingResosurce = new ActivityIdlingResosurce(mainActivity);
idlingBlock(activityIdlingResosurce, () -> {
onData(withId(R.id.list_item_wallet_card_tv_balance)).check(matches(withText("0.100")));
});
}
}
ActivityWithMockingNetworkUITest.java
public abstract class ActivityWithMockingNetworkUITest<T extends Activity> {
@Rule
public final ActivityTestRule<T> activityRule;
@Rule
public final TestName testNameRule = new TestName();
protected MockWebServer mockWebServer = new MockWebServer();
public ActivityWithMockingNetworkUITest() {
activityRule = new ActivityTestRule<>(provideActivityClass(), true, false);
}
protected abstract Class<T> provideActivityClass();
protected abstract Dispatcher provideDispatcher(String testName);
@Before
public void setup() throws IOException {
mockWebServer.start(8080);
mockWebServer.setDispatcher(provideDispatcher(testNameRule.getMethodName()));
Intents.init();
activityRule.launchActivity(null);
}
@After
public void teardown() throws IOException {
Intents.release();
mockWebServer.shutdown();
}
protected void idlingBlock(IdlingResource idlingResource, Runnable runnable) {
IdlingRegistry.getInstance().register(idlingResource);
runnable.run();
IdlingRegistry.getInstance().unregister(idlingResource);
}
}
SuccessAuthorizeDispatcher.java
public class SuccessAuthorizeDispatcher extends Dispatcher {
private Context context;
private Map<String, String> responseFilesByPath = new HashMap<>();
public SuccessAuthorizeDispatcher(Context context) {
this.context = context;
responseFilesByPath.put("/api/accounts", "account/get_0_01_balance_account.json");
}
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse errorResponse = new MockResponse().setResponseCode(401);
Uri parsed = Uri.parse(request.getPath());
if (parsed == null) { return errorResponse; }
String pathWithoutQueryParams = parsed.getPath();
String responseFile = responseFilesByPath.get(pathWithoutQueryParams);
if (responseFile != null) {
String responseBody = AssetReaderUtil.asset(context, responseFile);
return new MockResponse().setResponseCode(200).setBody(responseBody);
} else {
return errorResponse;
}
}
}