package Ġio . github . rob win . web . controller Ċ Ċ import Ġio . github . rob win . web . dto . Hero es Response Ċ import Ġio . github . rob win . web . entity . Hero Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . boot . test . web . client . Test Rest Template Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . test . context . junit 4 . Spring Runner Ċ Ċ @ RunWith ( Spring Runner :: class ) Ċ @ SpringBootTest ( web Environment Ġ= ĠSpring Boot Test . Web Environment . RANDOM _ PORT ) Ċ class ĠHero es Controller Test Ġ{ ĊĊĠ Ġ@ Autowired ĊĠ Ġprivate Ġlateinit Ġvar Ġrest Template : ĠTest Rest Template ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġget Hero es (){ ĊĠĠĠ Ġval Ġhero es Response Ġ= Ġrest Template . get For Entity ("/ api / hero es ", ĠHero es Response :: class . java ) ĊĠĠĠ ĠassertThat ( hero es Response . statusCode ). isEqualTo ( HttpStatus . OK ) ĊĠĠĠ ĠassertThat ( hero es Response . body . hero es ). has Size ( 1 0 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġcreate Hero (){ ĊĠĠĠ Ġval Ġhero Response Ġ= Ġrest Template . post For Entity ("/ api / hero es ", ĠHero ( name Ġ= Ġ" New Ġname "), ĠHero :: class . java ) ĊĠĠĠ ĠassertThat ( hero Response . statusCode ). isEqualTo ( HttpStatus . CREATED ) ĊĠĠĠ ĠassertThat ( hero Response . body ). isNotNull () ĊĠĠĠ ĠassertThat ( hero Response . body . id ). isNot EqualTo ( 0 ) ĊĠĠĠ ĠassertThat ( hero Response . body . name ). isEqualTo (" New Ġname ") ĊĠĠĠ ĠassertThat ( hero Response . headers [" location "]). isNotNull () ĊĊĠĠĠ Ġval Ġhero es Response Ġ= Ġrest Template . get For Entity ("/ api / hero es ", ĠHero es Response :: class . java ) ĊĠĠĠ ĠassertThat ( hero es Response . body . hero es ). has Size ( 1 1 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġget Hero ById (){ ĊĠĠĠ Ġval Ġhero Response Ġ= Ġrest Template . get For Entity ("/ api / hero es /{ id }", ĠHero :: class . java , Ġ 1 ) ĊĠĠĠ ĠassertThat ( hero Response . statusCode ). isEqualTo ( HttpStatus . OK ) ĊĠĠĠ ĠassertThat ( hero Response . body ). isNotNull () ĊĠĠĠ ĠassertThat ( hero Response . body . id ). isEqualTo ( 1 ) ĊĠĠĠ ĠassertThat ( hero Response . body . name ). isEqualTo (" Mr . ĠNice ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġdelete Hero ById (){ ĊĠĠĠ Ġrest Template . delete ("/ api / hero es /{ id }", Ġ 2 ) ĊĠĠĠ Ġval Ġhero Response Ġ= Ġrest Template . get For Entity ("/ api / hero es /{ id }", ĠHero :: class . java , Ġ 2 ) ĊĠĠĠ ĠassertThat ( hero Response . body ). isNotNull () ĊĠĠĠ ĠassertThat ( hero Response . statusCode ). isEqualTo ( HttpStatus . NOT _ FOUND ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġupdate Hero (){ ĊĠĠĠ Ġrest Template . put ("/ api / hero es /{ id }", ĠHero ( 3 , Ġ" Updated Ġname "), Ġ 3 ) ĊĠĠĠ Ġval Ġhero Response Ġ= Ġrest Template . get For Entity ("/ api / hero es /{ id }", ĠHero :: class . java , Ġ 3 ) ĊĠĠĠ ĠassertThat ( hero Response . statusCode ). isEqualTo ( HttpStatus . OK ) ĊĠĠĠ ĠassertThat ( hero Response . body . id ). isEqualTo ( 3 ) ĊĠĠĠ ĠassertThat ( hero Response . body . name ). isEqualTo (" Updated Ġname ") ĊĠ Ġ} Ċ } Ċ