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

Commit 674f1ea

Browse files
committed
testing
1 parent 76cd275 commit 674f1ea

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package graphql.spring.web.servlet.components;
22

3+
import graphql.ExecutionInput;
4+
import graphql.ExecutionResultImpl;
5+
import graphql.GraphQL;
36
import org.junit.Before;
47
import org.junit.Test;
58
import org.junit.runner.RunWith;
9+
import org.mockito.ArgumentCaptor;
10+
import org.mockito.Mockito;
611
import org.springframework.beans.factory.annotation.Autowired;
712
import org.springframework.http.MediaType;
813
import org.springframework.test.context.ContextConfiguration;
@@ -13,7 +18,12 @@
1318
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1419
import org.springframework.web.context.WebApplicationContext;
1520

21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
23+
import java.util.concurrent.CompletableFuture;
24+
1625
import static org.hamcrest.CoreMatchers.is;
26+
import static org.junit.Assert.assertThat;
1727
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
1828
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1929
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@@ -32,29 +42,50 @@ public class GraphQLControllerTest {
3242
@Autowired
3343
private WebApplicationContext wac;
3444

45+
@Autowired
46+
GraphQL graphql;
47+
3548
@Before
3649
public void setup() throws Exception {
3750
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
3851
}
3952

4053
@Test
4154
public void testGetRequest() throws Exception {
42-
String query = "{foo}";
43-
String variablesJson = "{\"key\":\"value\"}";
55+
String variablesJson = "{\"variable\":\"variableValue\"}";
56+
String query = "query myQuery {foo}";
57+
String operationName = "myQuery";
58+
59+
ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
60+
.data("bar")
61+
.build();
62+
CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
63+
ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
64+
Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
65+
4466

4567
MvcResult mvcResult = this.mockMvc.perform(get("/graphql")
4668
.param("query", query)
47-
.param("variables", variablesJson))
69+
.param("variables", variablesJson)
70+
.param("operationName", operationName))
4871
.andExpect(status().isOk())
4972
.andExpect(request().asyncStarted())
5073
.andReturn();
5174

5275
this.mockMvc.perform(asyncDispatch(mvcResult))
5376
.andDo(print()).andExpect(status().isOk())
5477
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
55-
.andExpect(jsonPath("data", is("foo")))
78+
.andExpect(jsonPath("data", is("bar")))
5679
.andReturn();
5780

81+
assertThat(captor.getAllValues().size(), is(1));
82+
83+
Map<String, Object> variables = new LinkedHashMap<>();
84+
variables.put("variable", "variableValue");
85+
assertThat(captor.getValue().getQuery(), is(query));
86+
assertThat(captor.getValue().getVariables(), is(variables));
87+
assertThat(captor.getValue().getOperationName(), is(operationName));
88+
5889
}
5990

6091
}

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

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

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import graphql.ExecutionInput;
5-
import graphql.ExecutionResultImpl;
64
import graphql.GraphQL;
7-
import org.mockito.ArgumentMatchers;
85
import org.mockito.Mockito;
96
import org.springframework.context.annotation.Bean;
107
import org.springframework.context.annotation.ComponentScan;
118
import org.springframework.context.annotation.Configuration;
129
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1310

14-
import java.util.concurrent.CompletableFuture;
15-
1611
@Configuration
1712
@EnableWebMvc
1813
@ComponentScan(basePackageClasses = GraphQLController.class)
@@ -22,11 +17,6 @@ public class TestAppConfig {
2217
@Bean
2318
public GraphQL graphQL() {
2419
GraphQL graphql = Mockito.mock(GraphQL.class);
25-
ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
26-
.data("foo")
27-
.build();
28-
CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
29-
Mockito.when(graphql.executeAsync(ArgumentMatchers.<ExecutionInput>any())).thenReturn(cf);
3020
return graphql;
3121
}
3222

0 commit comments

Comments
 (0)