1515 */
1616package io .spring .sample .graphql .project ;
1717
18- import java .util .List ;
19- import java .util .function .Consumer ;
20-
2118import org .junit .jupiter .api .BeforeEach ;
2219import org .junit .jupiter .api .Test ;
2320
2421import org .springframework .beans .factory .annotation .Autowired ;
2522import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
2623import org .springframework .boot .test .context .SpringBootTest ;
27- import org .springframework .core .ParameterizedTypeReference ;
28- import org .springframework .http .MediaType ;
24+ import org .springframework .graphql .test .query .GraphQLTester ;
2925import org .springframework .test .web .reactive .server .WebTestClient ;
3026import org .springframework .test .web .servlet .MockMvc ;
3127import org .springframework .test .web .servlet .client .MockMvcWebTestClient ;
3228
3329import static org .assertj .core .api .Assertions .assertThat ;
3430
3531/**
36- * Example tests using {@link WebTestClient} to connect to {@link MockMvc} as
37- * the "mock" server, i.e. without running an HTTP server.
32+ * GraphQL requests via {@link WebTestClient} connecting to {@link MockMvc}.
3833 */
3934@ SpringBootTest
4035@ AutoConfigureMockMvc
41- public class MockWebTestClientTests {
36+ public class MockMvcGraphQLTests {
37+
38+ private GraphQLTester graphQLTester ;
4239
43- private WebTestClient client ;
4440
4541 @ BeforeEach
4642 public void setUp (@ Autowired MockMvc mockMvc ) {
47- this .client = MockMvcWebTestClient .bindTo (mockMvc )
48- .baseUrl ("/graphql" )
49- .defaultHeaders (headers -> headers .setContentType (MediaType .APPLICATION_JSON ))
50- .build ();
43+ WebTestClient client = MockMvcWebTestClient .bindTo (mockMvc ).baseUrl ("/graphql" ).build ();
44+ this .graphQLTester = GraphQLTester .create (client );
5145 }
5246
47+
5348 @ Test
5449 void jsonPath () {
5550 String query = "{" +
@@ -60,13 +55,12 @@ void jsonPath() {
6055 " }" +
6156 "}" ;
6257
63- this .client .post ().bodyValue (JsonRequest .create (query ))
64- .exchange ()
65- .expectStatus ().isOk ()
66- .expectBody ().jsonPath ("$.data.project.releases[*].version" )
67- .value ((Consumer <List <String >>) versions -> {
68- assertThat (versions ).hasSizeGreaterThan (1 );
69- });
58+ this .graphQLTester .query (query )
59+ .execute ()
60+ .path ("project.releases[*].version" )
61+ .entityList (String .class )
62+ .hasSizeGreaterThan (1 );
63+
7064 }
7165
7266 @ Test
@@ -77,18 +71,10 @@ void jsonContent() {
7771 " }" +
7872 "}" ;
7973
80- String expectedJson = "{" +
81- " \" data\" :{" +
82- " \" project\" :{" +
83- " \" repositoryUrl\" :\" http://github.com/spring-projects/spring-framework\" " +
84- " }" +
85- " }" +
86- "}" ;
87-
88- this .client .post ().bodyValue (JsonRequest .create (query ))
89- .exchange ()
90- .expectStatus ().isOk ()
91- .expectBody ().json (expectedJson );
74+ this .graphQLTester .query (query )
75+ .execute ()
76+ .path ("project" )
77+ .matchesJson ("{\" repositoryUrl\" :\" http://github.com/spring-projects/spring-framework\" }" );
9278 }
9379
9480 @ Test
@@ -101,14 +87,11 @@ void decodedResponse() {
10187 " }" +
10288 "}" ;
10389
104- this .client .post ().bodyValue (JsonRequest .create (query ))
105- .exchange ()
106- .expectStatus ().isOk ()
107- .expectBody (new ParameterizedTypeReference <JsonResponse <Project >>() {})
108- .consumeWith (exchangeResult -> {
109- Project project = exchangeResult .getResponseBody ().getDataEntry ();
110- assertThat (project .getReleases ()).hasSizeGreaterThan (1 );
111- });
90+ this .graphQLTester .query (query )
91+ .execute ()
92+ .path ("project" )
93+ .entity (Project .class )
94+ .satisfies (project -> assertThat (project .getReleases ()).hasSizeGreaterThan (1 ));
11295 }
11396
11497}
0 commit comments