package Ġid . dev fest . poke dex Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġid . dev fest . poke dex . data . repository . Pokemon Repository Ċ import Ġid . dev fest . poke dex . model . Detail Ċ import Ġid . dev fest . poke dex . model . Pokemon Ċ import Ġid . dev fest . poke dex . model . Profile Ċ import Ġio . reactivex . Comp letable Ċ import Ġio . reactivex . Observable Ċ import Ġio . reactivex . Single Ċ import Ġio . reactivex . subjects . Publish Subject Ċ import Ġio . reactivex . subjects . Subject Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . rules . Test Rule Ċ import Ġorg . mockito . Argument Matchers Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito .* Ċ import Ġorg . mockito . Mockito Annotations Ċ Ċ class ĠMain ViewModel Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠviewModel : ĠMain ViewModel ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmock Pokemon Repository : ĠPokemon Repository ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġpokemon List Observer : ĠObserver < List < Pokemon >> ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġhas More Item Observer : ĠObserver < Boolean > ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġpokemon Observer : ĠObserver < Pokemon > ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġvar Ġrule : ĠTest Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ ĠMockito Annotations . init Mocks ( this ) ĊĊĠĠĠĠĠĠĠ Ġval Ġpokemon List Ġ= ĠlistOf < Pokemon >() ĊĠĠĠĠĠĠĠ Ġval Ġpokemon List Observable Ġ= ĠObservable . just ( pokemon List ) ĊĊĠĠĠĠĠĠĠ Ġ` when `( mock Pokemon Repository . listen To Pokemon List ()). thenReturn ( pokemon List Observable ) ĊĊĠĠĠĠĠĠĠ ĠviewModel Ġ= ĠMain ViewModel ( mock Pokemon Repository ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Pokemon List _ is Dis patched () Ġ{ ĊĠĠĠĠĠĠĠ Ġfun Ġgenerate Dummy Pokemon Data ( how M uch : ĠInt ): ĠList < Pokemon > Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġ( 0 Ġuntil Ġhow M uch ). toList (). map Ġ{ ĠPokemon ( it , Ġ" Pokemon -$ it ", Ġ" imageUrl -$ it ", Ġnull ) Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġ` when `( mock Pokemon Repository . listen To Pokemon List ()). thenReturn ( Observable . just ( generate Dummy Pokemon Data ( 2 0 ))) ĊĠĠĠĠĠĠĠ ĠviewModel Ġ= ĠMain ViewModel ( mock Pokemon Repository ) ĊĠĠĠĠĠĠĠ ĠviewModel . pokemon List . observe Fore ver ( pokemon List Observer ) ĊĊĠĠĠĠĠĠĠ Ġverify ( pokemon List Observer ). on Changed ( generate Dummy Pokemon Data ( 2 0 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Has More Pokemon _ is Dis patched () Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel . hasMore Pokemon . observe Fore ver ( hasMore Item Observer ) ĊĊĠĠĠĠĠĠĠ Ġ` when `( mock Pokemon Repository . fetch More Pokemon ( Argument Matchers . any Int ())). thenReturn ( Single . just ( false )) ĊĊĠĠĠĠĠĠĠ ĠviewModel . trigger Load More ( 0 ) ĊĠĠĠĠĠĠĠ Ġverify ( hasMore Item Observer ). on Changed ( false ) ĊĊĠĠĠĠĠĠĠ Ġ` when `( mock Pokemon Repository . fetch More Pokemon ( Argument Matchers . any Int ())). thenReturn ( Single . just ( true )) ĊĠĠĠĠĠĠĠ ĠviewModel . trigger Load More ( 0 ) ĊĊĠĠĠĠĠĠĠ Ġverify ( hasMore Item Observer , Ġtimes ( 2 )). on Changed ( true ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġobserve Pokemon With Id _ is Dis patched () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġpokemon Subject : ĠSubject < Pokemon > Ġ= ĠPublish Subject . create () ĊĠĠĠĠĠĠĠ Ġ` when `( mock Pokemon Repository . listen To Pokemon Id ( Argument Matchers . any Int ())). thenReturn ( pokemon Subject ) ĊĊĠĠĠĠĠĠĠ ĠviewModel . observe Pokemon With Id ( 0 ). observe Fore ver ( pokemon Observer ) ĊĊĠĠĠĠĠĠĠ Ġval Ġdispatch ing Pokemon Ġ= ĠPokemon ( 0 , Ġ" Pokemon - 0 ", Ġ" imageUrl - 0 ", Ġnull ) ĊĠĠĠĠĠĠĠ Ġpokemon Subject . onNext ( dispatch ing Pokemon ) ĊĊĠĠĠĠĠĠĠ Ġverify ( pokemon Observer ). on Changed ( dispatch ing Pokemon ) ĊĊĠĠĠĠĠĠĠ Ġval Ġanother Dispatch ing Pokemon Ġ= ĠPokemon ( 0 , Ġ" Pokemon - 0 ", Ġ" imageUrl - 0 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠDetail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf (" Gr ass "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf (" Nothing "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠProfile ( 1 , Ġ 1 , Ġ 1 ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of (" Base " Ġto Ġ 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġpokemon Subject . onNext ( another Dispatch ing Pokemon ) ĊĊĠĠĠĠĠĠĠ Ġverify ( pokemon Observer ). on Changed ( another Dispatch ing Pokemon ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfetch Pokemon Details _ is Correct () Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( mock Pokemon Repository . fetch Pokemon Detail ( 0 )). thenReturn ( Comp letable . complete ()) ĊĊĠĠĠĠĠĠĠ ĠviewModel . fetch Pokemon Details ( 0 ) ĊĠĠĠ Ġ} Ċ }