package Ġcom . mik ha el lope z . domain . use cases Ċ Ċ import Ġcom . mik ha el lope z . domain . exception . No Connected Exception Ċ import Ġcom . mik ha el lope z . domain . exception . Persistence Exception Ċ import Ġcom . mik ha el lope z . domain . model . Repo Ċ import Ġcom . mik ha el lope z . domain . repository . Repo Repository Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġio . reactivex . rx java 3 . core . Comp letable Ċ import Ġio . reactivex . rx java 3 . core . Single Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠGet List Repo Test Ġ{ ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġrepository : ĠRepo Repository ĊĊĠĠĠ Ġ// ĠProperties ĊĠĠĠ Ġprivate Ġval ĠuserName Ġ= Ġ" userName " ĊĊĠĠĠ Ġ// ĠUse ĠCase ĊĠĠĠ Ġprivate Ġval Ġuse Case Ġby Ġlazy Ġ{ ĠGet List Repo ( repository ) Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġbuild UseCase () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrepo List Ġ= Ġmock < List < Repo >>() ĊĊĠĠĠĠĠĠĠ Ġwhenever ( repository . isConnected ). thenReturn ( true ) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getList Repo ( userName )). thenReturn ( Single . just ( repo List )) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . save List Repo ( repo List )). thenReturn ( Comp letable . complete ()) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getCache List Repo ( userName )). thenReturn ( Single . just ( repo List )) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . sort List Repo ( repo List )). thenReturn ( Single . just ( repo List )) ĊĊĠĠĠĠĠĠĠ Ġuse Case . execute ( userName ). test () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Value Count ( 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Result ( repo List ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġbuild UseCase Without Network Connection () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrepo List Ġ= Ġmock < List < Repo >>() ĊĊĠĠĠĠĠĠĠ Ġwhenever ( repository . isConnected ). thenReturn ( false ) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getCache List Repo ( userName )). thenReturn ( Single . just ( repo List )) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getList Repo ( userName )). thenReturn ( Single . never ()) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . sort List Repo ( repo List )). thenReturn ( Single . just ( repo List )) ĊĊĠĠĠĠĠĠĠ Ġuse Case . execute ( userName ). test () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Value Count ( 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Result ( repo List ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġbuild UseCase Without Network Connection And Data Cache () Ġ{ ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . isConnected ). thenReturn ( false ) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getCache List Repo ( userName )). thenReturn ( Single . just ( emptyList ())) Ġ// Ġthrow ĠNo Connected Exception Ġhere ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getList Repo ( userName )). thenReturn ( Single . never ()) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . sort List Repo ( emptyList ())). thenReturn ( Single . just ( emptyList ())) ĊĊĠĠĠĠĠĠĠ Ġuse Case . execute ( userName ). test () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Error ( No Connected Exception :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġbuild UseCase With Persistence Exception () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrepo List Ġ= Ġmock < List < Repo >>() ĊĊĠĠĠĠĠĠĠ Ġwhenever ( repository . isConnected ). thenReturn ( true ) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getList Repo ( userName )). thenReturn ( Single . just ( repo List )) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . save List Repo ( repo List )). thenReturn ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠComp letable . error ( Persistence Exception ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġwhenever ( repository . getCache List Repo ( userName )). thenReturn ( Single . just ( emptyList ())) ĊĊĠĠĠĠĠĠĠ Ġuse Case . execute ( userName ). test () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Error ( Persistence Exception :: class . java ) ĊĠĠĠ Ġ} Ċ Ċ } Ċ