does not show any errors. assertEquals(username.getError(), null); assertEquals(password.getError(), null); instrumentation.runOnMainSync(new Runnable() { @Override public void run() { // Type a value into the username and password fields. username.setText("elvis"); password.setText(""); // Click the "login" button. login.performClick(); } }); instrumentation.waitForIdleSync(); // Verify error was shown only for password field. assertEquals(username.getError(), null); assertEquals(password.getError(), getString(R.string.required)); }
does not show any errors. assertEquals(username.getError(), null); assertEquals(password.getError(), null); instrumentation.runOnMainSync(new Runnable() { @Override public void run() { // Type a value into the username and password fields. username.setText("elvis"); password.setText(""); // Click the "login" button. login.performClick(); } }); instrumentation.waitForIdleSync(); // Verify error was shown only for password field. assertEquals(username.getError(), null); assertEquals(password.getError(), getString(R.string.required)); }
does not show any errors. assertThat(username).hasNoError(); assertThat(password).hasNoError(); // Type a value into the username and password fields. solo.typeText(username, "elvis"); solo.typeText(password, ""); // Click the "login" button. solo.clickOnView(login); // Verify error was shown only for username field. assertThat(username).hasNoError(); assertThat(password).hasError(R.string.required); }
does not show any errors. onView(withId(R.id.username)).check(matches(hasNoError())); onView(withId(R.id.password)).check(matches(hasNoError())); // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("elvis")); onView(withId(R.id.password)).perform(typeText("")); // Click the "login" button. onView(withId(R.id.login)).perform(click()); // Verify error was shown only for username field. onView(withId(R.id.password)).check(matches(hasNoError())); onView(withId(R.id.password)) .check(matches(hasError(R.string.required))); } https://code.google.com/p/android-test-kit/wiki/ EspressoV2CheatSheet
initial state does not show any errors. onView(withId(R.id.username)).check(matches(hasNoError())); onView(withId(R.id.password)).check(matches(hasNoError())); // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("elvis")); onView(withId(R.id.password)).perform(typeText("")); Spoon.screenshot(activity, "typed_text"); // Click the "login" button. onView(withId(R.id.login)).perform(click()); Spoon.screenshot(activity, "login_clicked"); // Verify error was shown only for username field. onView(withId(R.id.password)).check(matches(hasNoError())); onView(withId(R.id.password)) .check(matches(hasError(R.string.required))); }
initial state does not show any errors. onView(withId(R.id.username)).check(matches(hasNoError())); onView(withId(R.id.password)).check(matches(hasNoError())); // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("elvis")); onView(withId(R.id.password)).perform(typeText("")); Spoon.screenshot(activity, "typed_text"); // Click the "login" button. onView(withId(R.id.login)).perform(click()); Spoon.screenshot(activity, "login_clicked"); // Verify error was shown only for username field. onView(withId(R.id.password)).check(matches(hasNoError())); onView(withId(R.id.password)) .check(matches(hasError(R.string.required))); }
Analytics analytics = createAnalytics(); // 2. Test the code of interest. analytics.identify("elvis", new Traits() .putFirstName("Elvis") .putLastName("Presley") .putEmail("[email protected]")); // 3. Validate that we saw exactly what we wanted. PowerMockito.verifyStatic(); Localytics.setCustomerId("elvis"); PowerMockito.verifyStatic(); Localytics.setIdentifier("email", "[email protected]"); PowerMockito.verifyStatic(); Localytics.setIdentifier("customer_name", "Elvis Presley"); }
MockWebServer server = new MockWebServer(); // Schedule some responses. server.enqueue(new MockResponse().setBody("hello, world!")); // Start the server. server.start(); // Ask the server for its URL. You'll need this to make HTTP requests. URL baseUrl = server.getUrl(“/v1/chat/“); // Exercise your application code, which makes those HTTP requests. // Responses are returned in the same order that they are enqueued. Chat chat = new Chat(baseUrl); chat.loadMore(); assertThat(chat.messages()).containsExactly("hello, world!"); // Confirm that your app made the HTTP requests you were expecting. RecordedRequest request1 = server.takeRequest(); assertEquals("/v1/chat/messages/", request1.getPath()); assertThat(request1.getHeader("Authorization")).isNotNull(); // Shut down the server. server.shutdown(); }