15
15
*/
16
16
package io .spring .sample .graphql .project ;
17
17
18
- import java .util .List ;
19
- import java .util .function .Consumer ;
20
-
21
18
import org .junit .jupiter .api .BeforeEach ;
22
19
import org .junit .jupiter .api .Test ;
23
20
24
21
import org .springframework .beans .factory .annotation .Autowired ;
25
22
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
26
23
import 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 ;
29
25
import org .springframework .test .web .reactive .server .WebTestClient ;
30
26
import org .springframework .test .web .servlet .MockMvc ;
31
27
import org .springframework .test .web .servlet .client .MockMvcWebTestClient ;
32
28
33
29
import static org .assertj .core .api .Assertions .assertThat ;
34
30
35
31
/**
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}.
38
33
*/
39
34
@ SpringBootTest
40
35
@ AutoConfigureMockMvc
41
- public class MockWebTestClientTests {
36
+ public class MockMvcGraphQLTests {
37
+
38
+ private GraphQLTester graphQLTester ;
42
39
43
- private WebTestClient client ;
44
40
45
41
@ BeforeEach
46
42
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 );
51
45
}
52
46
47
+
53
48
@ Test
54
49
void jsonPath () {
55
50
String query = "{" +
@@ -60,13 +55,12 @@ void jsonPath() {
60
55
" }" +
61
56
"}" ;
62
57
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
+
70
64
}
71
65
72
66
@ Test
@@ -77,18 +71,10 @@ void jsonContent() {
77
71
" }" +
78
72
"}" ;
79
73
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\" }" );
92
78
}
93
79
94
80
@ Test
@@ -101,14 +87,11 @@ void decodedResponse() {
101
87
" }" +
102
88
"}" ;
103
89
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 ));
112
95
}
113
96
114
97
}
0 commit comments