package Ġid . r sd iz . the game db . mobile . favorite . vm Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġid . r sd iz . the game db . core . domain . model . Game Ċ import Ġid . r sd iz . the game db . core . domain . use case . game . Game UseCase Ċ import Ġid . r sd iz . the game db . mobile . favorite . Main Coroutine Scope Rule Ċ import Ġid . r sd iz . the game db . mobile . favorite . factory . Game Factory Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . flow . Flow Ċ import Ġkotlinx . coroutines . flow . flow Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Argument Captor Ċ import Ġorg . mockito . Captor Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ Ċ /** Ċ Ġ* ĠClass Ġfor Ġtesting Ġ[ Favorite ViewModel ] Ġclass Ċ Ġ*/ Ċ @ Experimental Cor outines Api Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠF avorite ViewModel Test Ġ{ ĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġinstant Executor Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġcoroutine Scope Ġ= ĠMain Coroutine Scope Rule () ĊĊĠĠĠ Ġprivate Ġval Ġfake Game List Ġ= ĠGame Factory . make Game List ( 1 0 ) ĊĠĠĠ Ġprivate Ġval Ġfavor ited Games Ġ= Ġfake Game List . filter Ġ{ Ġgame Ġ-> Ġgame . is Favorite Ġ} ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġfavorite ViewModel : ĠF avorite ViewModel ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġgame UseCase : ĠGame UseCase ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġgames Observer : ĠObserver < List < Game >> ĊĊĠĠĠ Ġ@ Captor ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġcapt or Games : ĠArgument Captor < List < Game >> ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠRun Ġbefore Ġstart Ġtesting ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġflow : ĠFlow < List < Game >> Ġ= Ġflow Ġ{ Ġemit ( f avor ited Games ) Ġ} ĊĊĠĠĠĠĠĠĠ ĠMockito .` when `( game UseCase . get Favorite Games ()). thenReturn ( flow ) ĊĠĠĠĠĠĠĠ Ġfavorite ViewModel Ġ= ĠF avorite ViewModel ( game UseCase ) ĊĠĠĠĠĠĠĠ ĠMockito . verify ( game UseCase ). get Favorite Games () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠFunction Ġfor Ġtesting Ġ[ Favorite ViewModel . favorite Games ] Ġvariable ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Favorite Games () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlive Data Ġ= Ġfavorite ViewModel . favorite Games ĊĠĠĠĠĠĠĠ Ġlive Data . observe Fore ver ( games Observer ) ĊĊĠĠĠĠĠĠĠ ĠMockito . verify ( games Observer ). on Changed ( capt or Games . capture ()) ĊĠĠĠĠĠĠĠ ĠassertNotNull ( capt or Games . value ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( f avor ited Games , Ġcapt or Games . value ) ĊĠĠĠ Ġ} Ċ } Ċ