Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Commit 9a40de5

Browse files
committed
testing
1 parent 357fa4a commit 9a40de5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

graphql-java-spring-webmvc/src/test/java/graphql/spring/web/servlet/components/GraphQLControllerTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package graphql.spring.web.servlet.components;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import graphql.ExecutionInput;
46
import graphql.ExecutionResultImpl;
57
import graphql.GraphQL;
@@ -26,6 +28,7 @@
2628
import static org.junit.Assert.assertThat;
2729
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
2830
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2932
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
3033
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3134
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -45,11 +48,62 @@ public class GraphQLControllerTest {
4548
@Autowired
4649
GraphQL graphql;
4750

51+
@Autowired
52+
ObjectMapper objectMapper;
53+
4854
@Before
4955
public void setup() throws Exception {
5056
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
5157
}
5258

59+
private String toJson(Map<String, Object> input) {
60+
try {
61+
return objectMapper.writeValueAsString(input);
62+
} catch (JsonProcessingException e) {
63+
throw new RuntimeException(e);
64+
}
65+
}
66+
67+
@Test
68+
public void testPostRequest() throws Exception {
69+
Map<String, Object> request = new LinkedHashMap<>();
70+
Map<String, Object> variables = new LinkedHashMap<>();
71+
variables.put("variable", "variableValue");
72+
String query = "query myQuery {foo}";
73+
request.put("query", query);
74+
request.put("variables", variables);
75+
String operationName = "myQuery";
76+
request.put("operationName", operationName);
77+
78+
ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
79+
.data("bar")
80+
.build();
81+
CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
82+
ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
83+
Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
84+
85+
86+
MvcResult mvcResult = this.mockMvc.perform(post("/graphql")
87+
.content(toJson(request))
88+
.contentType(MediaType.APPLICATION_JSON_UTF8))
89+
.andExpect(status().isOk())
90+
.andExpect(request().asyncStarted())
91+
.andReturn();
92+
93+
this.mockMvc.perform(asyncDispatch(mvcResult))
94+
.andDo(print()).andExpect(status().isOk())
95+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
96+
.andExpect(jsonPath("data", is("bar")))
97+
.andReturn();
98+
99+
assertThat(captor.getAllValues().size(), is(1));
100+
101+
assertThat(captor.getValue().getQuery(), is(query));
102+
assertThat(captor.getValue().getVariables(), is(variables));
103+
assertThat(captor.getValue().getOperationName(), is(operationName));
104+
105+
}
106+
53107
@Test
54108
public void testGetRequest() throws Exception {
55109
String variablesJson = "{\"variable\":\"variableValue\"}";

0 commit comments

Comments
 (0)