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

Android, multiDexEnabled = true --> not start Espresso tests (NoClassDefFoundError)
Android, Espresso. How check is soft keyboard is under view?
How to validate whether opened correct activity - Espresso
Espresso Test Failed: Wanted to match 1 intents. Actually matched 0 intents
How test onData() column value is not null?
Android, Espresso: Is one view is under another view?
Espresso: How fast show new activity?
Android, Espresso: Show/Hide view depends of item content
Android. Unit test. Check is text is show in correct language
Cucumber Espresso and Android : How to use view matchers to find the particular view using resource id?
how to re-run failed tests using AndroidJunit4?
Espresso: typeText - not work in cyrilic
Espresso test - not recognize ad
Espresso: How do i assert if a particular activity is launched when i click on a item using espresso
Android, Espresso: How check if tag value contains some string?
I want to scroll on recycler view till the element appears on screen, it may make network calls after each 10 elements.

Categories

HOME
rust
openshift
office365
nam
lambda
shinyapps
ontouchlistener
yii
kendo-dropdown
antd
yql
malware
google-webmaster-tools
supercollider
angular-meteor
pyresttest
tweepy
static-analysis
scheduler
kurento
timber
log4j2
fullcalendar-scheduler
freertos
smartgwt
xbox360
pywinauto
scala-ide
jaspersoft-studio
nose
tfsbuild
sqlplus
restier
quartz.net
tracing
offline-caching
math.js
windowsiot
private-key
subclassing
elastica
microsoft-ui-automation
stat
suds
coverage.py
yoast
google-prediction
web-inspector
sendinput
common-table-expression
jgrapht
pen
gmaps.js
openerp-6
xcode6.4
tablespace
traversable
fragmentstatepageradapter
adp
apache-pig-grunt
okta-api
decidable
opencyc
mousehover
android-studio-import
supersocket.net
ngcordova
phpredis
physicsjs
prism-4
python-curses
nanomsg
sunos
dos2unix
password-recovery
utf8-decode
xml-signature
nesper
berkeley-db-xml
.htpasswd
hyphen
jacob
flv
html-frames
web-frameworks
jquery-dialog
idispatch
getproperty
popup-blocker
coordinate
pcspim
flymake
glx
qtextbrowser
asio
drawimage
getopts
winsxs
sql-parametrized-query
git-log
turbine
ios-4.2
cassini
activestate
powergui
processors
tacit-programming
inversion

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App