Skip to content

Commit 0d52bfc

Browse files
committed
Update samples to use GraphQLTester
See gh-43
1 parent efb72c7 commit 0d52bfc

File tree

6 files changed

+100
-154
lines changed

6 files changed

+100
-154
lines changed

samples/webflux-websocket/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ dependencies {
1313
implementation 'org.springframework.boot:spring-boot-starter-webflux'
1414
implementation 'org.springframework.boot:spring-boot-starter-actuator'
1515
developmentOnly 'org.springframework.boot:spring-boot-devtools'
16+
testImplementation project(':spring-graphql-test')
17+
testImplementation 'org.springframework:spring-webflux'
1618
testImplementation 'org.springframework.boot:spring-boot-starter-test'
19+
testImplementation 'io.projectreactor:reactor-test'
1720
}
1821
test {
1922
useJUnitPlatform()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.spring.sample.graphql;
17+
18+
import graphql.GraphQL;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import reactor.core.publisher.Flux;
22+
import reactor.test.StepVerifier;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.graphql.WebGraphQLService;
27+
import org.springframework.graphql.test.query.GraphQLTester;
28+
29+
/**
30+
* GraphQL subscription tests directly via {@link GraphQL}.
31+
*/
32+
@SpringBootTest
33+
public class SubscriptionGraphQLTests {
34+
35+
private GraphQLTester graphQLTester;
36+
37+
38+
@BeforeEach
39+
public void setUp(@Autowired WebGraphQLService service) {
40+
this.graphQLTester = GraphQLTester.create(service);
41+
}
42+
43+
44+
@Test
45+
void subscriptionWithEntityPath() {
46+
String query = "subscription { greetings }";
47+
48+
Flux<String> result = this.graphQLTester.query(query)
49+
.executeSubscription()
50+
.toFlux("greetings", String.class);
51+
52+
StepVerifier.create(result)
53+
.expectNext("Hi", "Bonjour", "Hola", "Ciao", "Zdravo")
54+
.verifyComplete();
55+
}
56+
57+
@Test
58+
void subscriptionWithResponseSpec() {
59+
String query = "subscription { greetings }";
60+
61+
Flux<GraphQLTester.ResponseSpec> result = this.graphQLTester.query(query)
62+
.executeSubscription()
63+
.toFlux();
64+
65+
StepVerifier.create(result)
66+
.consumeNextWith(spec -> spec.path("greetings").valueExists())
67+
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour\""))
68+
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola\""))
69+
.expectNextCount(2)
70+
.verifyComplete();
71+
}
72+
73+
}

samples/webmvc-http/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter-actuator'
1717
developmentOnly 'org.springframework.boot:spring-boot-devtools'
1818
runtimeOnly 'com.h2database:h2'
19+
testImplementation project(':spring-graphql-test')
1920
testImplementation 'org.springframework:spring-webflux'
2021
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2122
}

samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonRequest.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonResponse.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockWebTestClientTests.java renamed to samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockMvcGraphQLTests.java

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,36 @@
1515
*/
1616
package io.spring.sample.graphql.project;
1717

18-
import java.util.List;
19-
import java.util.function.Consumer;
20-
2118
import org.junit.jupiter.api.BeforeEach;
2219
import org.junit.jupiter.api.Test;
2320

2421
import org.springframework.beans.factory.annotation.Autowired;
2522
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2623
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;
2925
import org.springframework.test.web.reactive.server.WebTestClient;
3026
import org.springframework.test.web.servlet.MockMvc;
3127
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
3228

3329
import 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

Comments
 (0)