33import org .instancio .Instancio ;
44import org .instancio .Select ;
55import org .junit .jupiter .api .*;
6- import org .springframework .beans .factory .annotation .Autowired ;
76import org .springframework .boot .test .context .SpringBootTest ;
8- import org .springframework .boot .test .web .client . TestRestTemplate ;
7+ import org .springframework .boot .test .web .server . LocalServerPort ;
98import org .springframework .boot .testcontainers .service .connection .ServiceConnection ;
10- import org .springframework .test .context .DynamicPropertyRegistry ;
11- import org .springframework .test .context .DynamicPropertySource ;
9+ import org .springframework .test .web .reactive .server .WebTestClient ;
1210import org .testcontainers .containers .PostgreSQLContainer ;
1311import org .testcontainers .junit .jupiter .Container ;
1412import org .testcontainers .junit .jupiter .Testcontainers ;
1513import pl .redhat .samples .insurance .domain .Insurance ;
1614
15+ import java .util .List ;
16+
1717@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
1818@ Testcontainers
1919@ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
2020public class InsuranceControllerTests {
2121
22- @ Autowired
23- TestRestTemplate restTemplate ;
22+ @ LocalServerPort
23+ int port ;
24+
25+ WebTestClient webTestClient ;
26+
27+ @ BeforeEach
28+ void setUp () {
29+ webTestClient = WebTestClient .bindToServer ()
30+ .baseUrl ("http://localhost:" + port )
31+ .build ();
32+ }
2433
2534 @ Container
2635 @ ServiceConnection
@@ -33,9 +42,14 @@ void add() {
3342 Insurance insurance = Instancio .of (Insurance .class )
3443 .ignore (Select .field ("id" ))
3544 .create ();
36- insurance = restTemplate .postForObject ("/insurances" , insurance , Insurance .class );
37- Assertions .assertNotNull (insurance );
38- Assertions .assertNotNull (insurance .getId ());
45+ Insurance saved = webTestClient .post ().uri ("/insurances" )
46+ .bodyValue (insurance )
47+ .exchange ()
48+ .expectStatus ().isOk ()
49+ .expectBody (Insurance .class )
50+ .returnResult ().getResponseBody ();
51+ Assertions .assertNotNull (saved );
52+ Assertions .assertNotNull (saved .getId ());
3953 }
4054
4155 @ Test
@@ -45,25 +59,42 @@ void updateAndGet() {
4559 Insurance insurance = Instancio .of (Insurance .class )
4660 .set (Select .field ("id" ), id )
4761 .create ();
48- restTemplate .put ("/insurances" , insurance );
49- Insurance updated = restTemplate .getForObject ("/insurances/{id}" , Insurance .class , id );
50- Assertions .assertNotNull (insurance );
51- Assertions .assertNotNull (insurance .getId ());
52- Assertions .assertEquals (id , insurance .getId ());
62+ webTestClient .put ().uri ("/insurances" )
63+ .bodyValue (insurance )
64+ .exchange ();
65+ Insurance updated = webTestClient .get ().uri ("/insurances/{id}" , id )
66+ .exchange ()
67+ .expectStatus ().isOk ()
68+ .expectBody (Insurance .class )
69+ .returnResult ().getResponseBody ();
70+ Assertions .assertNotNull (updated );
71+ Assertions .assertNotNull (updated .getId ());
72+ Assertions .assertEquals (id , updated .getId ());
5373 }
5474
5575 @ Test
5676 @ Order (3 )
5777 void getAll () {
58- Insurance [] insurances = restTemplate .getForObject ("/insurances" , Insurance [].class );
59- Assertions .assertEquals (1 , insurances .length );
78+ List <Insurance > insurances = webTestClient .get ().uri ("/insurances" )
79+ .exchange ()
80+ .expectStatus ().isOk ()
81+ .expectBodyList (Insurance .class )
82+ .hasSize (8 )
83+ .returnResult ().getResponseBody ();
84+ Assertions .assertNotNull (insurances );
85+ Assertions .assertEquals (8 , insurances .size ());
6086 }
6187
6288 @ Test
6389 @ Order (4 )
6490 void deleteAndGet () {
65- restTemplate .delete ("/insurances/{id}" , 1 );
66- Insurance insurance = restTemplate .getForObject ("/insurances/{id}" , Insurance .class , 1 );
91+ webTestClient .delete ().uri ("/insurances/{id}" , 1 )
92+ .exchange ()
93+ .expectStatus ().isOk ();
94+ Insurance insurance = webTestClient .get ().uri ("/insurances/{id}" , 1 )
95+ .exchange ()
96+ .expectBody (Insurance .class )
97+ .returnResult ().getResponseBody ();
6798 Assertions .assertNull (insurance );
6899 }
69100
0 commit comments