package Ġcom . vj g arc ia . ch uck nor ris j okes . data Ċ Ċ import Ġcom . vj g arc ia . ch uck nor ris j okes . data . network . Ch uck Nor ris ApiService Ċ import Ġcom . vj g arc ia . ch uck nor ris j okes . data . network . Jo ke Dto Ċ import Ġokhttp 3 . Ok HttpClient Ċ import Ġokhttp 3 . mock webserver . Mock Response Ċ import Ġokhttp 3 . mock webserver . Mock WebServer Ċ import Ġorg . junit . jupiter . api . After Each Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġretrofit 2 . Ret rofit Ċ import Ġretrofit 2 . adapter . rx java 2 . Rx Java 2 Call Adapter Factory Ċ import Ġretrofit 2 . converter . mos hi . M oshi Converter Factory Ċ import Ġjava . net . Http URLConnection Ċ Ċ class ĠCh uck Nor ris ApiService IT Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġmock WebServer Ġ= ĠMock WebServer () ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsut : ĠCh uck Nor ris ApiService ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġmock WebServer . start () ĊĠĠĠĠĠĠĠ Ġval Ġbase Endpoint Ġ= Ġmock WebServer . url ("/") ĊĊĠĠĠĠĠĠĠ Ġsut Ġ= ĠRet rofit . Builder () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. baseUrl ( base Endpoint ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. add Converter Factory ( M oshi Converter Factory . create ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. add Call Adapter Factory ( Rx Java 2 Call Adapter Factory . create ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. client ( Ok HttpClient . Builder (). build ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. create ( Ch uck Nor ris ApiService :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` random Ġjo ke Ġsuccess Ġresponse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġgiven AR andom Jo ke Successful Response () ĊĊĠĠĠĠĠĠĠ Ġval Ġrandom Jo ke Response Ġ= Ġsut . random (). test () ĊĊĠĠĠĠĠĠĠ Ġrandom Jo ke Response . assert Value ( Jo ke Dto ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġid Ġ= Ġ"_ en 0 5 aq csv uu 3 v 2 v op n oga ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtext Ġ= Ġ" While Ġur in ating , ĠCh uck ĠNor ris Ġis Ġeasily Ġcapable Ġof Ġwel ding Ġt itan ium .", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcategories Ġ= ĠlistOf (" science ") ĊĠĠĠĠĠĠĠ Ġ )). dispose () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` categories Ġsuccess Ġresponse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġgiven AC ategories Successful Response () ĊĊĠĠĠĠĠĠĠ Ġval Ġcategories Response Ġ= Ġsut . categories (). test () ĊĊĠĠĠĠĠĠĠ Ġval Ġexpected Categories Ġ= ĠlistOf (" animal ", Ġ" care er ", Ġ" ce le br ity ", Ġ" dev ", Ġ" explicit ", Ġ" f ashion ", Ġ" food ", Ġ" history ", Ġ" money ", Ġ" movie ", Ġ" music ", Ġ" polit ical ", Ġ" rel igion ", Ġ" science ", Ġ" sport ", Ġ" travel ") ĊĠĠĠĠĠĠĠ Ġcategories Response . assert Value ( expected Categories ). dispose () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġgiven AR andom Jo ke Successful Response () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresponse Body Ġ= Ġ""" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ{" categories ":[" science "]," created _ at ":" 2 0 2 0 - 0 1 - 0 5 Ġ 1 3 : 4 2 : 1 9 . 5 7 6 8 7 5 "," icon _ url ":" https :// assets . ch uck nor ris . host / img / avatar / ch uck - nor ris . png "," id ":" _ en 0 5 aq csv uu 3 v 2 v op n oga "," updated _ at ":" 2 0 2 0 - 0 1 - 0 5 Ġ 1 3 : 4 2 : 1 9 . 5 7 6 8 7 5 "," url ":" https :// api . ch uck nor ris . io / j okes /_ en 0 5 aq csv uu 3 v 2 v op n oga "," value ":" While Ġur in ating , ĠCh uck ĠNor ris Ġis Ġeasily Ġcapable Ġof Ġwel ding Ġt itan ium ." } ĊĠĠĠĠĠĠĠ Ġ""" . trim Indent () ĊĠĠĠĠĠĠĠ Ġmock WebServer . enqueue ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠMock Response () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Response Code ( Http URLConnection . HTTP _ OK ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Body ( response Body ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġgiven AC ategories Successful Response () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresponse Body Ġ= Ġ""" [" animal "," care er "," ce le br ity "," dev "," explicit "," f ashion "," food "," history "," money "," movie "," music "," polit ical "," rel igion "," science "," sport "," travel "] """ ĊĠĠĠĠĠĠĠ Ġmock WebServer . enqueue ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠMock Response () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Response Code ( Http URLConnection . HTTP _ OK ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Body ( response Body ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ After Each ĊĠĠĠ Ġfun ĠtearDown () Ġ{ ĊĠĠĠĠĠĠĠ Ġmock WebServer . shutdown () ĊĠĠĠ Ġ} Ċ }