package Ġcom . coding test . ul Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġcom . coding test . ul . di . D agger Test App Component Ċ import Ġcom . coding test . ul . di . Test App Module Ċ import Ġcom . coding test . ul . network . response . University Ċ import Ġcom . coding test . ul . repository . University Repository Ċ import Ġcom . coding test . ul . ui . view model . University ViewModel Ċ import Ġcom . coding test . ul . util . Network State Ċ import Ġcom . coding test . ul . util . Status Ċ import Ġcom . coding test . util . Test Coroutine Rule Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito . verify Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ import Ġorg . mockito . kotlin . do Return Ċ import Ġorg . mockito . kotlin . do Throw Ċ import Ġorg . mockito . kotlin . times Ċ import Ġjavax . inject . Inject ĊĊ Ċ @ Experimental Cor outines Api Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠUniversity ViewModel Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġinstant Executor Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġtest Coroutine Rule Ġ= ĠTest Coroutine Rule () ĊĊĠĠĠ Ġ@ Inject ĊĠĠĠ Ġlateinit Ġvar Ġun iversity Repository : ĠUniversity Repository ĊĊĠĠĠ Ġ@ Inject ĊĠĠĠ Ġlateinit Ġvar ĠviewModel : ĠUniversity ViewModel ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġobserver : ĠObserver < Network State > ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcomponent Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠD agger Test App Component ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. builder () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. test App Module ( Test App Module ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠĠĠ Ġcomponent . inject ( this ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven Server Response 2 0 0 _ when Fetch _ should Return Success () Ġ{ ĊĠĠĠĠĠĠĠ Ġtest Coroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdo Return ( ArrayList < University > ()). ` when `( university Repository ). get Network Un ivers ities () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdo Return ( ArrayList < University > ()). ` when `( university Repository ). getLocal Un ivers ities () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . getUn ivers ities (). observe Fore ver ( observer ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġverify ( university Repository ). get Network Un ivers ities () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġverify ( observer , Ġtimes ( 1 )). on Changed ( Network State ( Status . SUCCESS , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠArrayList < University > ())) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . getUn ivers ities (). remove Observer ( observer ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven Server ResponseError _ when Fetch _ should Return Error () Ġ{ ĊĠĠĠĠĠĠĠ Ġtest Coroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval ĠerrorMessage Ġ= Ġ" Unknown Ġerror " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdo Return ( ArrayList < University > ()). ` when `( university Repository ). getLocal Un ivers ities () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdo Throw ( RuntimeException ( errorMessage )). ` when `( university Repository ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. get Network Un ivers ities () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . getUn ivers ities (). observe Fore ver ( observer ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġverify ( university Repository ). get Network Un ivers ities () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġverify ( observer , Ġtimes ( 1 )). on Changed ( Network State ( Status . ERROR , ĠerrorMessage )) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . getUn ivers ities (). remove Observer ( observer ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }