package Ġcom . is db . login . data Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġcom . google . common . truth . Truth Ċ import Ġcom . is db . Test Coroutine Rule Ċ import Ġcom . is db . login . data . Result . Error Ċ import Ġcom . is db . login . data . Result . Success Ċ import Ġcom . is db . login . data . api . Login Service Ċ import Ġcom . is db . login . data . model . User Ċ import Ġio . mock k . co Every Ċ import Ġio . mock k . mock k Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġokhttp 3 . MediaType . Companion . to MediaType OrNull Ċ import Ġokhttp 3 . ResponseBody . Companion . to ResponseBody Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . ro bo lectric . Ro bo lectric TestRunner Ċ import Ġorg . ro bo lectric . annotation . Config Ċ import Ġretrofit 2 . Response Ċ import Ġjava . net . Socket TimeoutException Ċ Ċ @ Experimental Cor outines Api Ċ @ RunWith ( Ro bo lectric TestRunner :: class ) Ċ @ Config ( manifest Ġ= ĠConfig . NONE ) Ċ class ĠRegister Repository Test Ġ{ ĊĊĠ Ġ@ get : Rule ĊĠ Ġval Ġinstant Task Executor Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠ Ġ@ get : Rule ĊĠ Ġval Ġtest Coroutine Rule Ġ= ĠTest Coroutine Rule () ĊĊĠ Ġprivate Ġlateinit Ġvar Ġregister Repository : ĠRegister Repository ĊĠ Ġprivate Ġval Ġlogin Service : ĠLogin Service Ġ= Ġmock k () ĊĠ Ġprivate Ġval Ġuser Ġ= ĠUser ( username Ġ= Ġ" S ange et ", Ġpassword Ġ= Ġ" s ange et ", Ġemail Ġ= Ġ" s ange et @ gmail . com ") ĊĊĠ Ġ@ Before ĊĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠ Ġregister Repository Ġ= ĠRegister Repository ( login Service ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` login Ġuser Ġwhen Ġsuccess Ġnetwork Ġcall `() Ġ= Ġtest Coroutine Rule . run Blocking Test Ġ{ ĊĠĠĠ Ġco Every Ġ{ Ġlogin Service . createUser ( user ) Ġ} Ġreturns ĠResponse . success ( user ) ĊĊĠĠĠ Ġval Ġregister Ġ= Ġregister Repository . register ( user ) ĊĊĠĠĠ ĠTr uth . assertThat ( register ). isNotNull () ĊĠĠĠ ĠTr uth . assertThat ( register ). isInstanceOf ( Success :: class . java ) ĊĠĠĠ ĠTr uth . assertThat ( register ). isEqualTo ( Success ( user )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` login Ġuser Ġwhen Ġfailed Ġnetwork Ġcall `() Ġ= Ġtest Coroutine Rule . run Blocking Test Ġ{ ĊĠĠĠ Ġco Every Ġ{ Ġlogin Service . createUser ( user ) Ġ} Ġreturns ĠResponse . error ( ĊĠĠĠĠĠĠ 4 0 0 , ĊĠĠĠĠĠ Ġ"{\" key \":[\" some stuff \"] }" ĊĠĠĠĠĠĠĠ Ġ. to ResponseBody (" application / json ". to MediaType OrNull ()) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġregister Ġ= Ġregister Repository . register ( user ) ĊĊĠĠĠ ĠTr uth . assertThat ( register ). isNotNull () ĊĠĠĠ ĠTr uth . assertThat ( register ). isInstanceOf ( Error :: class . java ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` login Ġuser Ġwhen Ġfailed Ġnetwork Ġcall Ġthrows Ġexception `() Ġ= Ġtest Coroutine Rule . run Blocking Test Ġ{ ĊĠĠĠ Ġco Every Ġ{ Ġlogin Service . createUser ( user ) Ġ} Ġthrows ĠSocket TimeoutException () ĊĊĠĠĠ Ġval Ġlogin Ġ= Ġregister Repository . register ( user ) ĊĊĠĠĠ ĠTr uth . assertThat ( login ). isNotNull () ĊĠĠĠ ĠTr uth . assertThat ( login ). isInstanceOf ( Error :: class . java ) ĊĠ Ġ} Ċ }