package Ġcom . dong fg . project . api . web . controller Ċ Ċ import Ġcom . dong fg . project . api . any Ċ import Ġcom . dong fg . project . api . common . config . Jack son Config Ċ import Ġcom . dong fg . project . api . common . util . to Format String Ċ import Ġcom . dong fg . project . api . model . Comment Ċ import Ġcom . dong fg . project . api . service . Comment Service Ċ import Ġcom . dong fg . project . api . web . payload . Generic Payload Ċ import Ġcom . fasterxml . jackson . databind . ObjectMapper Ċ import Ġorg . hamcrest . core . Is .` is ` Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . BDD Mockito . given Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . autoconfigure . web . servlet . Web Mvc Test Ċ import Ġorg . springframework . boot . test . mock . mockito . Mock Bean Ċ import Ġorg . springframework . context . annotation . Import Ċ import Ġorg . springframework . http . MediaType Ċ import Ġorg . springframework . test . context . Active Profiles Ċ import Ġorg . springframework . test . context . junit 4 . Spring Runner Ċ import Ġorg . springframework . test . web . servlet . MockMvc Ċ import Ġorg . springframework . test . web . servlet . request . MockMvc Request Builders . get Ċ import Ġorg . springframework . test . web . servlet . request . MockMvc Request Builders . post Ċ import Ġorg . springframework . test . web . servlet . result . MockMvc Result Matchers . json Path Ċ import Ġorg . springframework . test . web . servlet . result . MockMvc Result Matchers . status Ċ import Ġjava . time . LocalDateTime Ċ Ċ @ RunWith ( Spring Runner :: class ) Ċ @ Web Mvc Test ( Comment Controller :: class , Ġsecure Ġ= Ġfalse ) Ċ @ Import ( Jack son Config :: class ) Ċ @ Active Profiles (" test ") Ċ class ĠComment Controller Test Ġ{ ĊĊĠĠĠ Ġ@ Mock Bean ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġcomment Service : ĠComment Service ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmock Mvc : ĠMock Mvc ĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠobjectMapper : ĠObjectMapper ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcomments () Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠcreateTime Ġ= ĠLocalDateTime . now () ĊĠĠĠĠĠĠĠ Ġgiven ( comment Service . comments ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. willReturn ( array ListOf ( Comment ( name Ġ= Ġ" junit 4 ", Ġcomment Ġ= Ġ" hello ", ĠcreateTime Ġ= ĠcreateTime ))) ĊĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( get ("/ comment "). accept ( MediaType . APPLICATION _ JSON _ UTF 8 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path ("$ [ 0 ]. name ", Ġ` is ` (" junit 4 "))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path ("$ [ 0 ]. comment ", Ġ` is ` (" hello "))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path ("$ [ 0 ]. create Time ", Ġ` is `( create Time . to Format String ()))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcreate Comment () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcomment Ġ= ĠComment ( name Ġ= Ġ" junit 4 ", Ġcomment Ġ= Ġ" hello ") ĊĊĠĠĠĠĠĠĠ Ġgiven ( comment Service . create Comment ( any ())) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. willReturn ( Generic Payload ( true )) ĊĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġpost ("/ comment ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. contentType ( MediaType . APPLICATION _ JSON _ UTF 8 ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. content ( object Mapper . writeValue AsString ( comment )) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. success ", Ġ` is `( true ))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. code ", Ġ` is `( 0 ))) ĊĠĠĠ Ġ} Ċ }