package Ġde . mobile j . k test Ċ Ċ import Ġandroid . arch . lifecycle . Lifecycle Ċ import Ġde . mobile j . k mock it . Mock Lifecycle Owner Ċ import Ġde . mobile j . k mock it . every Ċ import Ġde . mobile j . k mock it . mock up Ċ import Ġde . mobile j . wh item agic . advanced AsyncTask Mock Mode Ċ import Ġjunit . framework . Assert . assertEquals Ċ import Ġmock it . Mock ed Ċ import Ġmock it . integration . junit 4 . J Mock it Ċ import Ġokhttp 3 .* Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith ĊĊ Ċ @ RunWith ( J Mock it :: class ) Ċ class ĠSite Details ViewModel Test Ġ{ ĊĊĠĠĠ Ġ@ Mock ed ĊĠĠĠ Ġlateinit Ġvar Ġmock HttpClient : ĠOk HttpClient ĊĊĠĠĠ Ġ@ Mock ed ĊĠĠĠ Ġlateinit Ġvar Ġmock Response : ĠResponse ĊĊĠĠĠ Ġ@ Mock ed ĊĠĠĠ Ġlateinit Ġvar Ġmock Call : ĠCall ĊĊĠĠĠ Ġ@ Mock ed ĊĠĠĠ Ġlateinit Ġvar Ġmock Body : ĠResponse Body ĊĊĠĠĠ Ġ@ Mock ed ĊĠĠĠ Ġlateinit Ġvar Ġmock Callbacks : ĠSite Details ViewModel . Action Callbacks ĊĊĠĠĠ Ġ@ Mock ed ĊĠĠĠ Ġlateinit Ġvar Ġmock Builder : ĠOk HttpClient . Builder ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Basic Flow () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġmock up < Ok HttpClient . Builder > Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġon ( force Fn Type < Ok HttpClient . Builder .( Interceptor ) Ġ-> ĠOk HttpClient . Builder >( Ok HttpClient . Builder :: add Network Interceptor )) Ġ{ Ġmock Builder Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġon ( Ok HttpClient . Builder :: build ) Ġ{ Ġmock HttpClient Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġmock Call . execute () Ġ} Ġreturns Ġ{ Ġmock Response Ġ} ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġmock HttpClient . new Call ( any ( Request :: class . java )) Ġ} Ġreturns Ġ{ Ġmock Call Ġ} ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġmock Response . is Successful Ġ} Ġreturns Ġ{ Ġtrue Ġ} ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġmock Response . body () Ġ} Ġreturns Ġ{ Ġmock Body Ġ} ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġmock Body . string () Ġ} Ġreturns Ġ{ Ġ" the _ response " Ġ} ĊĊĠĠĠĠĠĠĠ Ġadvanced AsyncTask Mock Mode Ġ= Ġtrue ĊĊĠĠĠĠĠĠĠ Ġval Ġmock App Ġ= ĠApp () ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠSite Details ViewModel ( mock App ) ĊĊĠĠĠĠĠĠĠ Ġval Ġlifecycle Owner Ġ= ĠMock Lifecycle Owner () ĊĠĠĠĠĠĠĠ Ġsut . attach ( lifecycle Ġ= Ġlifecycle Owner ) ĊĊĠĠĠĠĠĠĠ Ġsut . setAction Callbacks ( mock Callbacks ) ĊĊĠĠĠĠĠĠĠ Ġlifecycle Owner . getMock Lifecycle (). handle Lifecycle Event ( Lifecycle . Event . ON _ RE SU ME ) ĊĊĠĠĠĠĠĠĠ Ġsut . set Site Url (" http :// www . test . com ") ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( sut . result . get (), Ġ" the _ response ") ĊĠĠĠ Ġ} Ċ }