11package my .bookshop ;
22
33import static cds .gen .catalogservice .CatalogService_ .REVIEWS ;
4- import static org .hamcrest .CoreMatchers .containsString ;
5- import static org .hamcrest .CoreMatchers .not ;
6- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
7- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
8- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
9- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
4+ import static org .assertj .core .api .Assertions .assertThat ;
105
116import cds .gen .catalogservice .Reviews ;
127import com .sap .cds .ql .Delete ;
138import com .sap .cds .services .persistence .PersistenceService ;
149import org .junit .jupiter .api .AfterEach ;
1510import org .junit .jupiter .api .Test ;
1611import org .springframework .beans .factory .annotation .Autowired ;
12+ import org .springframework .boot .resttestclient .autoconfigure .AutoConfigureRestTestClient ;
1713import org .springframework .boot .test .context .SpringBootTest ;
18- import org .springframework .boot .webmvc .test .autoconfigure .AutoConfigureMockMvc ;
1914import org .springframework .http .MediaType ;
2015import org .springframework .security .test .context .support .WithMockUser ;
21- import org .springframework .test .web .servlet .MockMvc ;
16+ import org .springframework .test .web .servlet .client . RestTestClient ;
2217
2318@ SpringBootTest
24- @ AutoConfigureMockMvc
19+ @ AutoConfigureRestTestClient
2520class CatalogServiceITest {
2621
2722 private static final String booksURI = "/api/browse/Books" ;
@@ -32,7 +27,7 @@ class CatalogServiceITest {
3227 private static final String USER_USER_STRING = "user" ;
3328 private static final String ADMIN_USER_STRING = "admin" ;
3429
35- @ Autowired private MockMvc mockMvc ;
30+ @ Autowired private RestTestClient client ;
3631
3732 @ Autowired private PersistenceService db ;
3833
@@ -42,47 +37,76 @@ void cleanup() {
4237 }
4338
4439 @ Test
45- void discountApplied () throws Exception {
46- mockMvc
47- .perform (get (booksURI + "?$filter=stock gt 200&top=1" ))
48- .andExpect (status ().isOk ())
49- .andExpect (jsonPath ("$.value[0].title" ).value (containsString ("11% discount" )));
40+ void discountApplied () {
41+ client
42+ .get ()
43+ .uri (booksURI + "?$filter=stock gt 200&top=1" )
44+ .exchange ()
45+ .expectStatus ()
46+ .isOk ()
47+ .expectBody ()
48+ .jsonPath ("$.value[0].title" )
49+ .value (String .class , title -> assertThat (title ).contains ("11% discount" ));
5050 }
5151
5252 @ Test
53- void discountNotApplied () throws Exception {
54- mockMvc
55- .perform (get (booksURI + "?$filter=stock lt 100&top=1" ))
56- .andExpect (status ().isOk ())
57- .andExpect (jsonPath ("$.value[0].title" ).value (not (containsString ("11% discount" ))));
53+ void discountNotApplied () {
54+ client
55+ .get ()
56+ .uri (booksURI + "?$filter=stock lt 100&top=1" )
57+ .exchange ()
58+ .expectStatus ()
59+ .isOk ()
60+ .expectBody ()
61+ .jsonPath ("$.value[0].title" )
62+ .value (String .class , title -> assertThat (title ).doesNotContain ("11% discount" ));
5863 }
5964
6065 @ Test
61- void createReviewNotAuthenticated () throws Exception {
66+ void createReviewNotAuthenticated () {
6267 String payload = createTestReview ().toJson ();
63- mockMvc
64- .perform (post (addReviewURI ).contentType (MediaType .APPLICATION_JSON ).content (payload ))
65- .andExpect (status ().isUnauthorized ());
68+ client
69+ .post ()
70+ .uri (addReviewURI )
71+ .contentType (MediaType .APPLICATION_JSON )
72+ .body (payload )
73+ .exchange ()
74+ .expectStatus ()
75+ .isUnauthorized ();
6676 }
6777
6878 @ Test
6979 @ WithMockUser (USER_USER_STRING )
70- void createReviewByUser () throws Exception {
80+ void createReviewByUser () {
7181 String payload = createTestReview ().toJson ();
72- mockMvc
73- .perform (post (addReviewURI ).contentType (MediaType .APPLICATION_JSON ).content (payload ))
74- .andExpect (status ().isOk ())
75- .andExpect (jsonPath ("$.createdBy" ).value (USER_USER_STRING ));
82+ client
83+ .post ()
84+ .uri (addReviewURI )
85+ .contentType (MediaType .APPLICATION_JSON )
86+ .body (payload )
87+ .exchange ()
88+ .expectStatus ()
89+ .isOk ()
90+ .expectBody ()
91+ .jsonPath ("$.createdBy" )
92+ .isEqualTo (USER_USER_STRING );
7693 }
7794
7895 @ Test
7996 @ WithMockUser (ADMIN_USER_STRING )
80- void createReviewByAdmin () throws Exception {
97+ void createReviewByAdmin () {
8198 String payload = createTestReview ().toJson ();
82- mockMvc
83- .perform (post (addReviewURI ).contentType (MediaType .APPLICATION_JSON ).content (payload ))
84- .andExpect (status ().isOk ())
85- .andExpect (jsonPath ("$.createdBy" ).value (ADMIN_USER_STRING ));
99+ client
100+ .post ()
101+ .uri (addReviewURI )
102+ .contentType (MediaType .APPLICATION_JSON )
103+ .body (payload )
104+ .exchange ()
105+ .expectStatus ()
106+ .isOk ()
107+ .expectBody ()
108+ .jsonPath ("$.createdBy" )
109+ .isEqualTo (ADMIN_USER_STRING );
86110 }
87111
88112 private Reviews createTestReview () {
0 commit comments