Add GraphQlTester test auto-configuration

This commit introduces the auto-configuration infrastructure for testing
Spring GraphQL applications with mock/embedded servers.

The new `@AutoConfigureGraphQlTester` annotation can contribute a
`GraphQlTester` bean to the test context for testing the application.

Closes gh-46
This commit is contained in:
Brian Clozel
2021-05-31 21:35:54 +02:00
parent 56917d8d83
commit 847c10be27
11 changed files with 624 additions and 14 deletions

View File

@@ -15,36 +15,28 @@
*/
package io.spring.sample.graphql.project;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.graphql.boot.test.tester.AutoConfigureGraphQlTester;
import org.springframework.graphql.test.tester.GraphQlTester;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* GraphQL requests via {@link WebTestClient} connecting to {@link MockMvc}.
* GraphQL requests via {@link GraphQlTester} connecting to {@link MockMvc}.
*/
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureGraphQlTester
public class MockMvcGraphQlTests {
@Autowired
private GraphQlTester graphQlTester;
@BeforeEach
public void setUp(@Autowired MockMvc mockMvc) {
WebTestClient client = MockMvcWebTestClient.bindTo(mockMvc).baseUrl("/graphql").build();
this.graphQlTester = GraphQlTester.create(client);
}
@Test
void jsonPath() {
String query = "{" +