-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGraphQLTest.java
More file actions
83 lines (65 loc) · 2.6 KB
/
GraphQLTest.java
File metadata and controls
83 lines (65 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.example.demo;
import static com.github.skjolber.mockito.graphql.matchers.ArgumentMatchers.queryName;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.HttpResponseException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.springframework.web.server.ResponseStatusException;
import com.github.skjolber.mockito.rest.spring.MockitoEndpointExtension;
import com.github.skjolber.mockito.rest.spring.api.MockEndpoint;
@ExtendWith(MockitoEndpointExtension.class)
@SpringBootTest
public class GraphQLTest {
@Autowired
private MyGraphQLClient client;
@MockEndpoint(path = "/api")
private MyGraphQLApi myGraphQLApi;
@Test
public void getSuccessful() throws Exception {
// setup mocking
String response = IOUtils.resourceToString("/responses/graphql-response.json", StandardCharsets.UTF_8);
when(myGraphQLApi.request(queryName("getTask"))).thenReturn(response);
// make the call
String title = client.getTask();
// verify result
assertThat(title).isEqualTo("GraphQL docs example");
verify(myGraphQLApi, times(1)).request(anyString());
}
@Test
public void getQueryNotMatched() throws Exception {
// somewhat crude testing to return null, but this demonstrates the use of matchers
// setup mocking
String response = IOUtils.resourceToString("/responses/graphql-response.json", StandardCharsets.UTF_8);
when(myGraphQLApi.request(queryName("getSomeOtherTask"))).thenReturn(response);
// make the call
try {
client.getTask();
} catch(Exception e) {
// pass
}
}
@Test
public void getHttpResponseCodes() throws Exception {
// somewhat crude testing to return null, but this demonstrates the use of matchers
// setup mocking
when(myGraphQLApi.request(queryName("getTask"))).thenThrow(new ResponseStatusException(HttpStatusCode.valueOf(400)));
// make the call
try {
client.getTask();
} catch(WebClientResponseException e) {
// pass
assertEquals(e.getStatusCode(), HttpStatusCode.valueOf(400));
}
}
}