package Ġbiz . eld mand ate . kotlin sample Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġbiz . eld mand ate . kotlin sample . api . RestClient Ċ import Ġbiz . eld mand ate . kotlin sample . model . N asa Ap od Ċ import Ġbiz . eld mand ate . kotlin sample . ui . home . Home ViewModel Ċ import Ġbiz . eld mand ate . kotlin sample . utils . LiveData Result Ċ import Ġjunit . framework . Assert . assertEquals Ċ import Ġjunit . framework . Assert . assertNotNull Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . junit . runners . JUnit 4 Ċ import Ġorg . mockito . Argument Matchers Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito .* Ċ import Ġorg . mockito . Mockito Annotations Ċ import Ġretrofit 2 . Response Ċ import Ġretrofit 2 . Response . error ĊĊ Ċ /** Ċ Ġ* ĠCreated Ġby ĠV ag hel a ĠM ith un ĠR . Ġon Ġ 2 1 / 1 0 / 2 0 . Ċ Ġ* ĠSmart ĠCall ĠCenter ĠSolution ĠP vt . ĠLtd . Ċ Ġ* Ġm ith un @ smart advanced tech . com Ċ Ġ*/ Ċ Ċ @ RunWith ( JUnit 4 :: class ) Ċ class ĠHome ViewModel Test Ġ{ ĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġinstant Task Executor Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġrest Client : ĠRest Client ĊĊĠĠĠ Ġlateinit Ġvar Ġhome ViewModel : ĠHome ViewModel ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ ĠMockito Annotations . init Mocks ( this ) ĊĠĠĠĠĠĠĠ Ġhome ViewModel Ġ= ĠHome ViewModel () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfetch Positive Response () Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( rest Client . getN asa Ap od Data ( Argument Matchers . any String ())). then Answer Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ then Answer ĠResponse . success ( N asa Ap od ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġobserver Ġ= Ġmock ( Observer :: class . java ) Ġas ĠObserver < LiveData Result < N asa Ap od >> ĊĠĠĠĠĠĠĠ Ġthis . home ViewModel . nasa Ap od . observe Fore ver ( observer ) ĊĊĠĠĠĠĠĠĠ Ġthis . home ViewModel . getN asa Ap od Data ( Argument Matchers . any String ()) ĊĊĠĠĠĠĠĠĠ ĠassertNotNull ( this . home ViewModel . nasa Ap od . value ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( LiveData Result . Status . SUCCESS , Ġthis . home ViewModel . nasa Ap od . value ?. status ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfetch Repositories _ error () Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( rest Client . getN asa Ap od Data ( Argument Matchers . any String ())). then Answer Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ then Answer Ġerror < N asa Ap od >( 5 0 0 , Ġnull ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġobserver Ġ= Ġmock ( Observer :: class . java ) Ġas ĠObserver < LiveData Result < N asa Ap od >> ĊĠĠĠĠĠĠĠ Ġthis . home ViewModel . nasa Ap od . observe Fore ver ( observer ) ĊĊĠĠĠĠĠĠĠ Ġthis . home ViewModel . getN asa Ap od Data ( Argument Matchers . any String ()) ĊĊĠĠĠĠĠĠĠ ĠassertNotNull ( this . home ViewModel . nasa Ap od . value ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( LiveData Result . Status . ERROR , Ġthis . home ViewModel . nasa Ap od . value ?. status ) ĊĠĠĠĠĠĠĠ Ġassert ( this . home ViewModel . nasa Ap od . value ?. err Ġis ĠThrowable ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġset Loading Visibility _ on Success () Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( rest Client . getN asa Ap od Data ( com . n ha ar man . mock it ok otlin 2 . any ())). then Answer Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ then Answer ĠResponse . success ( N asa Ap od ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġspi ed ViewModel Ġ= Ġcom . n ha ar man . mock it ok otlin 2 . spy ( this . home ViewModel ) ĊĊĠĠĠĠĠĠĠ Ġspi ed ViewModel . getN asa Ap od Data ( Argument Matchers . any String ()) ĊĠĠĠĠĠĠĠ Ġverify ( sp ied ViewModel , Ġtimes ( 1 )). set Loading Visibility ( Argument Matchers . any Boolean ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġset Loading Visibility _ onError () Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( rest Client . getN asa Ap od Data ( Argument Matchers . any String ())). then Answer Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ then Answer Ġerror < N asa Ap od >( 5 0 0 , Ġnull ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġspi ed ViewModel Ġ= Ġcom . n ha ar man . mock it ok otlin 2 . spy ( this . home ViewModel ) ĊĊĠĠĠĠĠĠĠ Ġspi ed ViewModel . getN asa Ap od Data ( Argument Matchers . any String ()) ĊĠĠĠĠĠĠĠ Ġverify ( sp ied ViewModel , Ġtimes ( 2 )). set Loading Visibility ( Argument Matchers . any Boolean ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġset Loading Visibility _ on No Data () Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( rest Client . getN asa Ap od Data ( Argument Matchers . any String ())). then Answer Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ then Answer ĠResponse . success ( N asa Ap od ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġspi ed ViewModel Ġ= Ġcom . n ha ar man . mock it ok otlin 2 . spy ( this . home ViewModel ) ĊĊĠĠĠĠĠĠĠ Ġspi ed ViewModel . getN asa Ap od Data ( Argument Matchers . any String ()) ĊĠĠĠĠĠĠĠ Ġverify ( sp ied ViewModel , Ġtimes ( 2 )). set Loading Visibility ( Argument Matchers . any Boolean ()) ĊĠĠĠ Ġ} Ċ }