package Ġmock ifer . example Ċ Ċ import Ġandroid . support . test . espress o . Esp ress o . on View Ċ import Ġandroid . support . test . espress o . Esp ress o . press Back Ċ import Ġandroid . support . test . espress o . action . View Actions Ċ import Ġandroid . support . test . espress o . action . View Actions . click Ċ import Ġandroid . support . test . espress o . assertion . View Assertions . matches Ċ import Ġandroid . support . test . espress o . contrib . RecyclerView Actions . action On Item At Position Ċ import Ġandroid . support . test . espress o . matcher . Root Matchers . is Dialog Ċ import Ġandroid . support . test . espress o . matcher . View Matchers .* Ċ import Ġandroid . support . test . runner . Android JUnit 4 Ċ import Ġandroid . support . v 7 . widget . RecyclerView Ċ import Ġandroid . view . View Ċ import Ġmock ifer . Mock ifer Ċ import Ġmock ifer . example . RecyclerView Count Assertion . Companion . with ItemCount Ċ import Ġmock ifer . example . android . R Ċ import Ġmock ifer . example . android . main . MainActivity Ċ import Ġorg . hamcrest . Core Matchers . all Of Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ class ĠExample Instrument ed Test Ġ{ ĊĠĠĠ Ġ@ Jvm Field Ġ@ Rule Ġvar Ġtest Rule : ĠMock ifer Test Rule < *> Ġ= ĠMock ifer Test Rule ( MainActivity :: class . java ) ĊĊĠĠĠ Ġ@ Before Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ ĠMock ifer . reset () ĊĠĠĠĠĠĠĠ ĠMock ifer . clear Active Mocks () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġview Mock C ats () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// ĠBefore Ġbeginning Ġthe Ġtest , Ġwe Ġwill Ġpre configure Ġsome Ġmocks Ġby Ġpushing Ġthem Ġon ĠMock ifer . ĊĠĠĠĠĠĠĠ Ġ// ĠThere Ġare Ġalso Ġmethods Ġto Ġpush Ġa Ġmock Ġto Ġbe Ġrun Ġmore Ġthan Ġonce , Ġor Ġpushing Ġa Ġcollection ĊĠĠĠĠĠĠĠ Ġ// Ġof Ġmocks Ġto Ġsave Ġtime Ġin Ġsetup Ġ( they Ġwill Ġall Ġdefault Ġto Ġrunning Ġonce Ġeach ). ĊĠĠĠĠĠĠĠ ĠMock ifer . push Mock (" mocks . cats . get fake cats ") ĊĊĠĠĠĠĠĠĠ Ġlaunch Activity () ĊĊĠĠĠĠĠĠĠ Ġ// ĠSelect Ġthe Ġfirst Ġcat Ġand Ġverify Ġit Ġwas Ġthe Ġmock Ġthat Ġwe Ġpushed . ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . re cyclerView ), Ġis Displayed ())). perform ( action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . cat Name EditText )). check ( matches ( with Text (" Mock y Ġcat Ġ 1 "))) ĊĊĠĠĠĠĠĠĠ Ġ// ĠMove Ġback Ġagain , Ġand Ġthis Ġtime Ġthe Ġmock Ġwould Ġhave Ġbeen Ġconsumed Ġso Ġthe ĊĠĠĠĠĠĠĠ Ġ// Ġregular Ġdata Ġwould Ġload . ĊĠĠĠĠĠĠĠ Ġpress Back () ĊĊĠĠĠĠĠĠĠ Ġ// ĠSelect Ġthe Ġfirst Ġcat Ġand Ġverify Ġit Ġwas Ġthe Ġdefault Ġcat Ġthat Ġisn 't Ġmocked . ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . re cyclerView ), Ġis Displayed ())). perform ( action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . cat Name EditText )). check ( matches ( with Text (" S mis h "))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġedit Cat () Ġ{ ĊĠĠĠĠĠĠĠ Ġlaunch Activity () ĊĊĠĠĠĠĠĠĠ Ġ// ĠSelect Ġcat ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . re cyclerView ), Ġis Displayed ())). perform ( action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĊĠĠĠĠĠĠĠ Ġ// ĠUpdate Ġcat ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . cat Name EditText )). check ( matches ( with Text (" S mis h "))) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . cat Name EditText )). perform ( View Actions . replace Text (" Updated Ġkit ty ")) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . save Button )). perform ( click ()) ĊĊĠĠĠĠĠĠĠ Ġ// ĠRe select Ġsame Ġcat ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . re cyclerView ), Ġis Displayed ())). perform ( action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . cat Name EditText )). check ( matches ( with Text (" Updated Ġkit ty "))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġdelete Cat () Ġ{ ĊĠĠĠĠĠĠĠ Ġlaunch Activity () ĊĊĠĠĠĠĠĠĠ Ġ// ĠCheck Ġthat Ġwe Ġstart Ġwith Ġ 4 Ġcats ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . re cyclerView )). check ( with ItemCount ( 4 )) ĊĊĠĠĠĠĠĠĠ Ġ// ĠSelect Ġfirst Ġcat ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . re cyclerView ), Ġis Displayed ())). perform ( action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĊĠĠĠĠĠĠĠ Ġ// ĠDelete Ġcat ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . cat Name EditText )). check ( matches ( with Text (" S mis h "))) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . delete Button )). perform ( click ()) ĊĊĠĠĠĠĠĠĠ Ġ// ĠCheck Ġthat Ġwe Ġnow Ġhave Ġ 3 Ġcats ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . re cyclerView )). check ( with ItemCount ( 3 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġsimulate Internal 5 0 0 Error () Ġ{ ĊĠĠĠĠĠĠĠ ĠMock ifer . push Mock (" mocks . cats . get cats . error ") ĊĠĠĠĠĠĠĠ Ġlaunch Activity () ĊĊĠĠĠĠĠĠĠ Ġon View ( with Text (" Network ĠError ")) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĊĠĠĠĠĠĠĠ Ġon View ( with Text (" OK ")) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( click ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġlaunch Activity () Ġ{ ĊĠĠĠĠĠĠĠ Ġtest Rule . launch Activity ( null ) ĊĠĠĠ Ġ} Ċ } Ċ