11package graphql .spring .web .servlet .components ;
22
3+ import graphql .ExecutionInput ;
4+ import graphql .ExecutionResultImpl ;
5+ import graphql .GraphQL ;
36import org .junit .Before ;
47import org .junit .Test ;
58import org .junit .runner .RunWith ;
9+ import org .mockito .ArgumentCaptor ;
10+ import org .mockito .Mockito ;
611import org .springframework .beans .factory .annotation .Autowired ;
712import org .springframework .http .MediaType ;
813import org .springframework .test .context .ContextConfiguration ;
1318import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
1419import org .springframework .web .context .WebApplicationContext ;
1520
21+ import java .util .LinkedHashMap ;
22+ import java .util .Map ;
23+ import java .util .concurrent .CompletableFuture ;
24+
1625import static org .hamcrest .CoreMatchers .is ;
26+ import static org .junit .Assert .assertThat ;
1727import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .asyncDispatch ;
1828import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
1929import 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}
0 commit comments