11package zin .rashidi .boot .data .mongodb .tm .user ;
22
33import org .junit .jupiter .api .Test ;
4- import org .springframework .beans .factory .annotation .Autowired ;
5- import org .springframework .boot .resttestclient .autoconfigure .AutoConfigureTestRestTemplate ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .boot .resttestclient .autoconfigure .AutoConfigureRestTestClient ;
66import org .springframework .boot .test .context .SpringBootTest ;
7- import org .springframework .boot .resttestclient .TestRestTemplate ;
87import org .springframework .boot .testcontainers .service .connection .ServiceConnection ;
9- import org .springframework .http .HttpEntity ;
10- import org .springframework .http .HttpHeaders ;
8+ import org .springframework .test .web .servlet .client .RestTestClient ;
119import org .testcontainers .junit .jupiter .Container ;
12- import org .testcontainers .junit .jupiter .Testcontainers ;
10+ import org .testcontainers .junit .jupiter .Testcontainers ;
1311import org .testcontainers .mongodb .MongoDBContainer ;
1412
15- import static org .assertj .core .api .Assertions .assertThat ;
1613import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment .RANDOM_PORT ;
17- import static org .springframework .http .HttpMethod .POST ;
1814import static org .springframework .http .MediaType .APPLICATION_JSON ;
1915import static zin .rashidi .boot .data .mongodb .tm .user .User .Status .ACTIVE ;
2016
2117/**
2218 * @author Rashidi Zin
2319 */
24- @ AutoConfigureTestRestTemplate
20+ @ AutoConfigureRestTestClient
2521@ SpringBootTest (webEnvironment = RANDOM_PORT )
2622@ Testcontainers
2723class CreateUserTests {
@@ -31,22 +27,24 @@ class CreateUserTests {
3127 private static final MongoDBContainer mongo = new MongoDBContainer ("mongo:latest" ).withReplicaSet ();
3228
3329 @ Autowired
34- private TestRestTemplate restTemplate ;
30+ private RestTestClient restTemplate ;
3531
3632 @ Test
3733 void create () {
38- var headers = new HttpHeaders () {{ setContentType (APPLICATION_JSON ); }};
3934 var body = """
4035 {
4136 "username": "rashidi.zin",
4237 "name": "Rashidi Zin"
4338 }
4439 """ ;
4540
46- var response = restTemplate .exchange ("/users" , POST , new HttpEntity <>(body , headers ), User .class );
47- var createdUser = response .getBody ();
48-
49- assertThat (createdUser ).extracting ("status" ).isEqualTo (ACTIVE );
41+ restTemplate .post ().uri ("/users" )
42+ .contentType (APPLICATION_JSON )
43+ .body (body )
44+ .exchange ()
45+ .expectStatus ().isCreated ()
46+ .expectBody ()
47+ .jsonPath ("$.status" ).isEqualTo ("ACTIVE" );
5048 }
5149
5250}
0 commit comments