Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 613878f

Browse files
refactor: test refactoring
1 parent 02d8319 commit 613878f

File tree

5 files changed

+119
-104
lines changed

5 files changed

+119
-104
lines changed

graphql-spring-boot-test-autoconfigure/src/test/java/com/graphql/spring/boot/test/GraphQLTestAutoConfigurationTestBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void assertThatTestSubscriptionWorksCorrectly() {
2222
// WHEN - THEN
2323
testSubscription.start("test-subscription.graphql")
2424
.awaitAndGetNextResponse(1000)
25+
.assertThatNoErrorsArePresent()
2526
.assertThatField("$.data.testSubscription").asString().isEqualTo(FOO);
2627
}
2728

@@ -30,7 +31,7 @@ void assertThatTestTemplateAutoConfigurationWorksCorrectly() throws IOException
3031
final GraphQLTestTemplate testTemplate = applicationContext.getBean(GraphQLTestTemplate.class);
3132
// WHEN - THEN
3233
testTemplate.postForResource("test-query.graphql")
33-
.assertThatNumberOfErrors().isZero().and()
34+
.assertThatNoErrorsArePresent()
3435
.assertThatField("$.data.testQuery").asString().isEqualTo(FOO);
3536
}
3637
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertIsNotPresentTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ private static Stream<String> valuesThatShouldCauseFailure() {
2020
return Stream.of(null, NON_NULL_VALUE);
2121
}
2222

23-
@ParameterizedTest
23+
@ParameterizedTest(name = "value = {0}")
2424
@MethodSource("valuesThatShouldCauseFailure")
25-
@DisplayName("Should fail if the value at the provided path is null.")
26-
void shouldFailIfNull(final String value) {
25+
@DisplayName("Should fail if the value at the provided path is present.")
26+
void shouldFailIfValueIsPresent(final String value) {
2727
// GIVEN
2828
given(graphQLResponse.getRaw(MOCK_PATH)).willReturn(value);
2929
final GraphQLFieldAssert graphQLFieldAssert = new GraphQLFieldAssert(graphQLResponse, MOCK_PATH);

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLResponseTest.java

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
import java.util.List;
3333
import java.util.stream.Stream;
3434

35-
import static org.assertj.core.api.Assertions.assertThat;
36-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
35+
import static org.assertj.core.api.Assertions.*;
3736

3837
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
3938
public class GraphQLResponseTest {
4039

41-
private static final String DATA_PATH = "$.data.test";
40+
private static final String DATA_PATH_TEST = "$.data.test";
41+
private static final String INNER_FOO_LIST_DATA_PATH = "$.data.externalList[*].fooList";
42+
private static final String NESTED_LIST_RESPONSE_FILE = "response-with-nested-list.json";
43+
private static final String EMPTY_OBJECT_RESPONSE = "{}";
4244

4345
@Autowired
4446
private ObjectMapper objectMapper;
@@ -80,84 +82,78 @@ private static Stream<Arguments> testGetListArguments() {
8082
);
8183
}
8284

85+
private static String loadClassPathResource(final String path) {
86+
try(InputStream resourceStream = new ClassPathResource(path).getInputStream()) {
87+
return StreamUtils.copyToString(resourceStream, StandardCharsets.UTF_8);
88+
} catch (IOException e) {
89+
fail("Test setup error - failed to load test resource.", e);
90+
return "";
91+
}
92+
}
93+
8394
@DisplayName("Should get the JSON node's value as a String.")
8495
@ParameterizedTest
8596
@MethodSource("testGetStringArguments")
86-
public void testGetString(
87-
final String bodyString,
88-
final String expected
89-
) {
97+
public void testGetString(final String bodyString, final String expected) {
9098
//GIVEN
91-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper);
99+
final GraphQLResponse graphQLResponse = createResponse(bodyString);
92100
//WHEN
93-
final String actual = graphQLResponse.get(DATA_PATH);
101+
final String actual = graphQLResponse.get(DATA_PATH_TEST);
94102
//THEN
95103
assertThat(actual).isEqualTo(expected);
96104
}
97105

98106
@DisplayName("Should get the JSON node's value as an instance of a specified class.")
99107
@ParameterizedTest
100108
@MethodSource("testGetArguments")
101-
public <T> void testGet(
102-
final String bodyString,
103-
final Class<T> clazz,
104-
final T expected
105-
) {
109+
public <T> void testGet(final String bodyString, final Class<T> clazz, final T expected) {
106110
//GIVEN
107-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper);
111+
final GraphQLResponse graphQLResponse = createResponse(bodyString);
108112
//WHEN
109-
final T actual = graphQLResponse.get(DATA_PATH, clazz);
113+
final T actual = graphQLResponse.get(DATA_PATH_TEST, clazz);
110114
//THEN
111115
assertThat(actual).isInstanceOf(clazz).isEqualTo(expected);
112116
}
113117

114118
@DisplayName("Should get the JSON node's value as a List.")
115119
@ParameterizedTest
116120
@MethodSource("testGetListArguments")
117-
public <T> void testGetList(
118-
final String bodyString,
119-
final Class<T> clazz,
120-
final List<T> expected
121-
) {
121+
public <T> void testGetList(final String bodyString, final Class<T> clazz, final List<T> expected) {
122122
//GIVEN
123-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(bodyString), objectMapper);
123+
final GraphQLResponse graphQLResponse = createResponse(bodyString);
124124
//WHEN
125-
final List<T> actual = graphQLResponse.getList(DATA_PATH, clazz);
125+
final List<T> actual = graphQLResponse.getList(DATA_PATH_TEST, clazz);
126126
//THEN
127127
assertThat(actual).containsExactlyElementsOf(expected);
128128
}
129129

130130
@DisplayName("Should get field as defined by Jackson's JavaType.")
131131
@Test
132-
public void testGetAsJavaType() throws IOException {
132+
public void testGetAsJavaType() {
133133
// GIVEN
134-
final String dataPath = "$.data.externalList[*].fooList";
135-
final String response = loadClassPathResource("response-with-nested-list.json");
136134
final List<List<String>> expected = Arrays.asList(Arrays.asList("foo1", "foo2"),
137135
Collections.singletonList("foo3"));
138136
final JavaType stringList = objectMapper.getTypeFactory().constructCollectionType(List.class, String.class);
139137
final JavaType listOfStringLists = objectMapper.getTypeFactory().constructCollectionType(List.class,
140138
stringList);
141139
// WHEN
142-
final List<List<String>> actual = new GraphQLResponse(ResponseEntity.ok(response), objectMapper)
143-
.get(dataPath, listOfStringLists);
140+
final List<List<String>> actual = createResponse(loadClassPathResource(NESTED_LIST_RESPONSE_FILE))
141+
.get(INNER_FOO_LIST_DATA_PATH, listOfStringLists);
144142
// THEN
145143
assertThat(actual).containsExactlyElementsOf(expected);
146144

147145
}
148146

149147
@DisplayName("Should throw illegal argument exception if type is incompatible")
150148
@Test
151-
public void testGetAsJavaTypeConversionError() throws IOException {
149+
public void testGetAsJavaTypeConversionError() {
152150
// GIVEN
153-
final String dataPath = "$.data.externalList[*].fooList";
154-
final String response = loadClassPathResource("response-with-nested-list.json");
155151
final JavaType stringType = objectMapper.getTypeFactory().constructType(String.class);
156152
// WHEN
157-
final GraphQLResponse actual = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
153+
final GraphQLResponse actual = createResponse(loadClassPathResource(NESTED_LIST_RESPONSE_FILE));
158154
// THEN
159155
assertThatExceptionOfType(IllegalArgumentException.class)
160-
.isThrownBy(() -> actual.get(dataPath, stringType));
156+
.isThrownBy(() -> actual.get(INNER_FOO_LIST_DATA_PATH, stringType));
161157

162158
}
163159

@@ -170,7 +166,7 @@ public void testGetAsJavaTypeConversionError() throws IOException {
170166
@DisplayName("Should pass the assertion if no errors are present.")
171167
void testExpectNoErrorsPass(final String response) {
172168
// WHEN
173-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
169+
final GraphQLResponse graphQLResponse = createResponse(response);
174170
// THEN
175171
assertThat(graphQLResponse.assertThatNoErrorsArePresent())
176172
.as("Should not throw an exception. Should return GraphQL response instance.")
@@ -180,112 +176,116 @@ void testExpectNoErrorsPass(final String response) {
180176
@Test
181177
@DisplayName("Should throw assertion error if an error is present in the response.")
182178
void testExpectNoErrorsFail() {
183-
// WHEN
179+
// GIVEN
184180
final String response = "{\"errors\": [{\"message\": \"Test error.\"}], \"data\": { \"foo\":\"bar\"} }";
185-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
181+
// WHEN
182+
final GraphQLResponse graphQLResponse = createResponse(response);
186183
// THEN
187184
assertThatExceptionOfType(AssertionError.class)
188185
.isThrownBy(graphQLResponse::assertThatNoErrorsArePresent)
189186
.withMessage("Expected no GraphQL errors, but got 1: <Unspecified error>: Test error.");
190187
}
191188

192189
@Test
193-
@DisplayName("Should return assertion for the number of errors.")
190+
@DisplayName("Should return an assertion for the number of errors.")
194191
void testNumberOfErrorsAssertion() {
195-
// WHEN
192+
// GIVEN
196193
final String response = "{\"errors\": [{\"message\": \"Test error.\"}, {\"message\": \"Test error 2.\"}], "
197194
+ "\"data\": { \"foo\":\"bar\"} }";
198-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
199-
// THEN
195+
final GraphQLResponse graphQLResponse = createResponse(response);
196+
// WHEN
200197
final NumberOfErrorsAssertion actual = graphQLResponse.assertThatNumberOfErrors();
198+
// THEN
201199
assertThat(actual).extracting("actual").isEqualTo(2);
202200
assertThat(actual.and()).isSameAs(graphQLResponse);
203201
}
204202

205203
@Test
206-
@DisplayName("Should return assertion for the list of errors.")
204+
@DisplayName("Should return an assertion for the list of errors.")
207205
void testErrorListAssertion() {
208206
// GIVEN
209207
final String response = "{\"errors\": [{\"message\": \"Test error.\", \"errorType\": \"DataFetchingException\"}], "
210208
+ "\"data\": { \"foo\":\"bar\"} }";
209+
final GraphQLResponse graphQLResponse = createResponse(response);
211210
// WHEN
212-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
213211
final GraphQLErrorListAssertion actual = graphQLResponse.assertThatListOfErrors();
214212
// THEN
215213
assertThat(actual).isNotNull();
216214
assertThat(actual).extracting("actual").isEqualTo(Collections.singletonList(GraphQLTestError.builder()
217-
.message("Test error.").errorType(ErrorType.DataFetchingException).build()));
215+
.message("Test error.").errorType(ErrorType.DataFetchingException).build()));
218216
assertThat(actual.and()).isSameAs(graphQLResponse);
219217
}
220218

221219
@Test
222-
@DisplayName("Should return a assertion for the json content of the response.")
220+
@DisplayName("Should return an assertion for the json content of the response.")
223221
void testFieldAssertion() {
224222
// GIVEN
225223
final String response = "{ \"data\": { \"foo\":\"bar\"} }";
224+
final GraphQLResponse graphQLResponse = createResponse(response);
226225
// WHEN
227-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
228226
final JsonContentAssert actual = graphQLResponse.assertThatJsonContent();
229227
// THEN
230228
assertThat(actual).isNotNull();
231229
assertThat(actual).extracting("actual").isEqualTo(response);
232230
}
233231

234232
@Test
235-
@DisplayName("Should return a assertion for the data field.")
233+
@DisplayName("Should return an assertion for the data field.")
236234
void testDataFieldAssertion() {
235+
// GIVEN
236+
final GraphQLResponse response = createResponse(EMPTY_OBJECT_RESPONSE);
237237
// WHEN
238-
final GraphQLResponse response = new GraphQLResponse(ResponseEntity.ok("{}"), objectMapper);
239238
final GraphQLFieldAssert actual = response.assertThatDataField();
240239
// THEN
241-
assertThat(actual).isNotNull();
242-
assertThat(actual).extracting("graphQLResponse", "jsonPath")
243-
.containsExactly(response, "$.data");
240+
assertThatAssertionIsCorrect(actual, response, "$.data");
244241
}
245242

246243
@Test
247-
@DisplayName("Should return a assertion for the errors field.")
244+
@DisplayName("Should return an assertion for the errors field.")
248245
void testErrorFieldAssertion() {
246+
// GIVEN
247+
final GraphQLResponse response = createResponse(EMPTY_OBJECT_RESPONSE);
249248
// WHEN
250-
final GraphQLResponse response = new GraphQLResponse(ResponseEntity.ok("{}"), objectMapper);
251249
final GraphQLFieldAssert actual = response.assertThatErrorsField();
252250
// THEN
253-
assertThat(actual).isNotNull();
254-
assertThat(actual).extracting("graphQLResponse", "jsonPath")
255-
.containsExactly(response, "$.errors");
251+
assertThatAssertionIsCorrect(actual, response, "$.errors");
256252
}
257253

258254
@Test
259-
@DisplayName("Should return a assertion for the extensions field.")
255+
@DisplayName("Should return an assertion for the extensions field.")
260256
void testErrorExtensionsAssertion() {
257+
// GIVEN
258+
final GraphQLResponse response = createResponse(EMPTY_OBJECT_RESPONSE);
261259
// WHEN
262-
final GraphQLResponse response = new GraphQLResponse(ResponseEntity.ok("{}"), objectMapper);
263260
final GraphQLFieldAssert actual = response.assertThatExtensionsField();
264261
// THEN
265-
assertThat(actual).isNotNull();
266-
assertThat(actual).extracting("graphQLResponse", "jsonPath")
267-
.containsExactly(response, "$.extensions");
262+
assertThatAssertionIsCorrect(actual, response, "$.extensions");
268263
}
269264

270265
@Test
271266
@DisplayName("Should return a field assertion for the specific field.")
272267
void testJsonAssertion() {
273268
// GIVEN
274-
final String response = "{}";
275269
final String path = "$any.path";
270+
final GraphQLResponse graphQLResponse = createResponse(EMPTY_OBJECT_RESPONSE);
276271
// WHEN
277-
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(response), objectMapper);
278272
final GraphQLFieldAssert actual = graphQLResponse.assertThatField(path);
279273
// THEN
280-
assertThat(actual).isNotNull();
281-
assertThat(actual).extracting("graphQLResponse", "jsonPath")
282-
.containsExactly(graphQLResponse, path);
274+
assertThatAssertionIsCorrect(actual, graphQLResponse, path);
283275
}
284276

285-
private String loadClassPathResource(String path) throws IOException {
286-
try(InputStream resourceStream = new ClassPathResource(path).getInputStream()) {
287-
return StreamUtils.copyToString(resourceStream, StandardCharsets.UTF_8);
288-
}
277+
private GraphQLResponse createResponse(final String s) {
278+
return new GraphQLResponse(ResponseEntity.ok(s), objectMapper);
279+
}
280+
281+
private void assertThatAssertionIsCorrect(
282+
final GraphQLFieldAssert actual,
283+
final GraphQLResponse expectedResponse,
284+
final Object expectedPath
285+
) {
286+
assertThat(actual).isNotNull();
287+
assertThat(actual).extracting("graphQLResponse", "jsonPath")
288+
.containsExactly(expectedResponse, expectedPath);
289289
}
290290

291291
@Data

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestErrorToStringTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import graphql.ErrorType;
44
import graphql.GraphQLError;
55
import graphql.language.SourceLocation;
6+
import org.junit.jupiter.api.DisplayName;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.provider.Arguments;
89
import org.junit.jupiter.params.provider.MethodSource;
@@ -15,57 +16,60 @@
1516

1617
public class GraphQLTestErrorToStringTest {
1718

19+
private static final String TEST_MESSAGE = "Test message";
20+
1821
private static Stream<Arguments> toStringTestArguments() {
1922
return Stream.of(
2023
Arguments.of(
2124
new GraphQLTestError(null, null, null, null, null),
2225
"<Unspecified error>: <error message not provided>"
2326
),
2427
Arguments.of(
25-
new GraphQLTestError("Test message", null, null, null, null),
28+
new GraphQLTestError(TEST_MESSAGE, null, null, null, null),
2629
"<Unspecified error>: Test message"
2730
),
2831
Arguments.of(
29-
new GraphQLTestError("Test message", null, ErrorType.DataFetchingException, null, null),
32+
new GraphQLTestError(TEST_MESSAGE, null, ErrorType.DataFetchingException, null, null),
3033
"DataFetchingException: Test message"
3134
),
3235
Arguments.of(
33-
new GraphQLTestError("Test message", Collections.emptyList(), ErrorType.DataFetchingException, null, null),
36+
new GraphQLTestError(TEST_MESSAGE, Collections.emptyList(), ErrorType.DataFetchingException, null, null),
3437
"DataFetchingException: Test message"
3538
),
3639
Arguments.of(
37-
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
40+
new GraphQLTestError(TEST_MESSAGE, Collections.singletonList(new SourceLocation(1, 2)),
3841
ErrorType.DataFetchingException, null, null),
3942
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source"
4043
),
4144
Arguments.of(
42-
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
45+
new GraphQLTestError(TEST_MESSAGE, Collections.singletonList(new SourceLocation(1, 2)),
4346
ErrorType.DataFetchingException, Collections.emptyList(), null),
4447
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source"
4548
),
4649
Arguments.of(
47-
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
50+
new GraphQLTestError(TEST_MESSAGE, Collections.singletonList(new SourceLocation(1, 2)),
4851
ErrorType.DataFetchingException, Arrays.asList("path", "to", "error"), null),
4952
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path/to/error"
5053
),
5154
Arguments.of(
52-
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2, "test.graphql")),
55+
new GraphQLTestError(TEST_MESSAGE, Collections.singletonList(new SourceLocation(1, 2, "test.graphql")),
5356
ErrorType.DataFetchingException, Arrays.asList("path", "to", "error"), null),
5457
"DataFetchingException: Test message at line 1, column 2 in test.graphql. Selection path: path/to/error"
5558
),
5659
Arguments.of(
57-
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
60+
new GraphQLTestError(TEST_MESSAGE, Collections.singletonList(new SourceLocation(1, 2)),
5861
ErrorType.DataFetchingException, Arrays.asList("path", 123, "error"), null),
5962
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path[123]/error"
6063
),
6164
Arguments.of(
62-
new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)),
65+
new GraphQLTestError(TEST_MESSAGE, Collections.singletonList(new SourceLocation(1, 2)),
6366
ErrorType.DataFetchingException, Arrays.asList("path", 123, "error"), Collections.singletonMap("please ignore", "this")),
6467
"DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path[123]/error"
6568
)
6669
);
6770
}
6871

72+
@DisplayName("toString should work properly")
6973
@ParameterizedTest(name = "{1}")
7074
@MethodSource("toStringTestArguments")
7175
void testGraphQLTestErrorToString(final GraphQLError error, final String expectedToString) {

0 commit comments

Comments
 (0)