android-espresso


Mock server requests Android Espresso UI Testing


I am using Espresso to write UI tests for my Android application and would like to mock http requests using MockWebServer.
I need to mock authentication responses and sign in the user before the tests are run.
Is there a way make the app use mockwebserver so that isntead of making actual requests, I can use respontes enqueued on mockwebserver.
So far I have:
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("some_email#test.com"), 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());
}
This example of replacing dependency with Dagger. But you can use any other approaches for DI. Main idea - replace dependency during test by providing a "test" version of you application via custom test runner.

Related Links

Espresso mockWebServer response does not intercept http calls
Android Instrumentation pull test results/files from device
Espresso hangs the UI and the System kills the application
Espresso test with phone asleep
Unable to run espresso test
How to force Espresso wait for Eventbus posts?
Espresso Web + Chrome cust tab
Espresso 2 - How to Test Multiple Activities?
Android Espresso test to check syncness between ViewPager's fragments and TabViews
Is there a way to automate API calls using Espresso?
Android Espresso - Single list with same items (AmbiguousViewMatcherException)
Do Matchers extending hamcrest.BaseMatcher have to be consistent
testing http connection on google cloud test lab devices
Espresso Test Recorder has detached from the device VM
Espresso - Using google espresso how to send email from Gmail app
Unable to perform PressBack in Espresso Test

Categories

HOME
spring-data
android-espresso
firebase
mc
svn
list
generics
barcode-scanner
transactions
subdomain
stacktrace.js
biztalk
semantic-web
cryptography
google-sheets-query
save
heuristics
locationmanager
responsivevoice
multi-step
stimulsoft
mahout-recommender
web-deployment
bsd
eclipse-emf
morris.js
echosign
npoi
pyspark-sql
splunk-query
intune
source-insight
argv
mongodb-3.4
appcode
directfb
busboy
redgate
cargo
aurelia-http-client
linq.js
swfupload
coverage.py
elasticsearch-aggregation
dbcc
wysihtml5
image-registration
photoswipe
beeline
lightning-workbench
entropy
winmerge
gnome-terminal
finder
yui3
ipywidgets
google-identity-toolkit
xcode6.4
mathjs
pyaudio
between
mathnet
vaadin4spring
gulp-uglify
seadragon
android-expansion-files
webgrind
ajax4jsf
godeps
chicagoboss
blank-line
compositetype
iodocs
lumia-imaging-sdk
coding-efficiency
distributed-r
.htpasswd
meteor-collections
webaii
wso2bam
meteorite
visual-c++-2005
eclim
progress-db
acitree
.net-remoting
web-frameworks
semicolon
android-authenticator
email-verification
tridion-2011
xalan
arrow
image-formats
first-class
type-safety
play2-mini
firefly-mv
sql-parametrized-query
webresponse
resharper-5.1
xap
activex-exe
chronic
rendering-engine

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App