package Ġapp . story tel . candidate . com . view model Ċ Ċ import Ġandroid . view . View Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġapp . story tel . candidate . com . repository . I Repository Ċ import Ġapp . story tel . candidate . com . repository . posts Mock Data Ċ import Ġapp . story tel . candidate . com . util . Rx Immediate Scheduler Rule Ċ import Ġapp . story tel . candidate . com . util . test Observer Ċ import Ġio . reactivex . Single . error Ċ import Ġio . reactivex . Single . just Ċ import Ġorg . junit . Assert Ċ 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 .` when ` Ċ import Ġorg . mockito . junit . Mockito JUnit Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ import Ġjava . lang . Thread . sleep Ċ Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠPost List ViewModel Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġmock ito Rule Ġ= ĠMockito JUnit . rule () ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġtask Executor Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġs chedulers Ġ= ĠRx Immediate Scheduler Rule () ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġrepository : ĠI Repository ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġvm : ĠPost List ViewModel ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġvm Ġ= ĠPost List ViewModel ( repository ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Posts And Photos Success Test () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġ` when `( repository . getPost And Photo ()). thenReturn ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġjust ( posts Mock Data ) ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġloading Values Ġ= Ġvm . loading Visibility . test Observer () ĊĠĠĠĠĠĠĠ Ġval Ġmain Layout Values Ġ= Ġvm . list Layout Visibility . test Observer () ĊĠĠĠĠĠĠĠ Ġval Ġno Conn Layout Values Ġ= Ġvm . no Conn Visibility . test Observer () ĊĠĠĠĠĠĠĠ Ġval Ġposts LiveData Ġ= Ġvm . get Posts And Photos LiveData (). test Observer () ĊĊĠĠĠĠĠĠĠ Ġ// then ĊĠĠĠĠĠĠĠ Ġ// ĠTODO : ĠFind Ġa Ġproper Ġway Ġto Ġwait Ġfor Ġrx java Ġsingle Ġcompletion Ġbefore Ġevaluate Ġthe Ġresults ĊĠĠĠĠĠĠĠ Ġsleep ( 1 0 0 ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( View . GONE , ĠView . VISIBLE , ĠView . GONE ), Ġloading Values . observed Value ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( View . GONE , ĠView . VISIBLE ), Ġmain Layout Values . observed Value ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( View . GONE , ĠView . GONE ), Ġno Conn Layout Values . observed Value ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( posts Mock Data ), Ġposts LiveData . observed Value ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Posts And Photos Loading Error Test () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġ` when `( repository . getPost And Photo ()). thenReturn ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġerror ( Exception (" exception ")) ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġloading Values Ġ= Ġvm . loading Visibility . test Observer () ĊĠĠĠĠĠĠĠ Ġval Ġmain Layout Values Ġ= Ġvm . list Layout Visibility . test Observer () ĊĠĠĠĠĠĠĠ Ġval Ġno Conn Layout Values Ġ= Ġvm . no Conn Visibility . test Observer () ĊĊĠĠĠĠĠĠĠ Ġvm . get Posts And Photos LiveData () ĊĊĠĠĠĠĠĠĠ Ġ// then ĊĠĠĠĠĠĠĠ Ġ// ĠTODO : ĠFind Ġa Ġproper Ġway Ġto Ġwait Ġfor Ġrx java Ġsingle Ġcompletion Ġbefore Ġevaluate Ġthe Ġresults ĊĠĠĠĠĠĠĠ Ġsleep ( 1 0 0 ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( View . GONE , ĠView . VISIBLE , ĠView . GONE ), Ġloading Values . observed Value ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( View . GONE , ĠView . GONE ), Ġmain Layout Values . observed Value ) ĊĠĠĠĠĠĠĠ ĠAssert . assertEquals ( listOf ( View . GONE , ĠView . VISIBLE ), Ġno Conn Layout Values . observed Value ) ĊĠĠĠ Ġ} Ċ } Ċ