Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions samples/webflux-websocket/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation project(':spring-graphql-test')
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
test {
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;

import graphql.GraphQL;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.graphql.WebGraphQLService;
import org.springframework.graphql.test.query.GraphQLTester;

/**
* GraphQL subscription tests directly via {@link GraphQL}.
*/
@SpringBootTest
public class SubscriptionGraphQLTests {

private GraphQLTester graphQLTester;


@BeforeEach
public void setUp(@Autowired WebGraphQLService service) {
this.graphQLTester = GraphQLTester.create(service);
}


@Test
void subscriptionWithEntityPath() {
String query = "subscription { greetings }";

Flux<String> result = this.graphQLTester.query(query)
.executeSubscription()
.toFlux("greetings", String.class);

StepVerifier.create(result)
.expectNext("Hi", "Bonjour", "Hola", "Ciao", "Zdravo")
.verifyComplete();
}

@Test
void subscriptionWithResponseSpec() {
String query = "subscription { greetings }";

Flux<GraphQLTester.ResponseSpec> result = this.graphQLTester.query(query)
.executeSubscription()
.toFlux();

StepVerifier.create(result)
.consumeNextWith(spec -> spec.path("greetings").valueExists())
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour\""))
.consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola\""))
.expectNextCount(2)
.verifyComplete();
}

}
1 change: 1 addition & 0 deletions samples/webmvc-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
testImplementation project(':spring-graphql-test')
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,36 @@
*/
package io.spring.sample.graphql.project;

import java.util.List;
import java.util.function.Consumer;

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.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.graphql.test.query.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;

/**
* Example tests using {@link WebTestClient} to connect to {@link MockMvc} as
* the "mock" server, i.e. without running an HTTP server.
* GraphQL requests via {@link WebTestClient} connecting to {@link MockMvc}.
*/
@SpringBootTest
@AutoConfigureMockMvc
public class MockWebTestClientTests {
public class MockMvcGraphQLTests {

private GraphQLTester graphQLTester;

private WebTestClient client;

@BeforeEach
public void setUp(@Autowired MockMvc mockMvc) {
this.client = MockMvcWebTestClient.bindTo(mockMvc)
.baseUrl("/graphql")
.defaultHeaders(headers -> headers.setContentType(MediaType.APPLICATION_JSON))
.build();
WebTestClient client = MockMvcWebTestClient.bindTo(mockMvc).baseUrl("/graphql").build();
this.graphQLTester = GraphQLTester.create(client);
}


@Test
void jsonPath() {
String query = "{" +
Expand All @@ -60,13 +55,12 @@ void jsonPath() {
" }" +
"}";

this.client.post().bodyValue(JsonRequest.create(query))
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.data.project.releases[*].version")
.value((Consumer<List<String>>) versions -> {
assertThat(versions).hasSizeGreaterThan(1);
});
this.graphQLTester.query(query)
.execute()
.path("project.releases[*].version")
.entityList(String.class)
.hasSizeGreaterThan(1);

}

@Test
Expand All @@ -77,18 +71,10 @@ void jsonContent() {
" }" +
"}";

String expectedJson = "{" +
" \"data\":{" +
" \"project\":{" +
" \"repositoryUrl\":\"http://github.com/spring-projects/spring-framework\"" +
" }" +
" }" +
"}";

this.client.post().bodyValue(JsonRequest.create(query))
.exchange()
.expectStatus().isOk()
.expectBody().json(expectedJson);
this.graphQLTester.query(query)
.execute()
.path("project")
.matchesJson("{\"repositoryUrl\":\"http://github.com/spring-projects/spring-framework\"}");
}

@Test
Expand All @@ -101,14 +87,11 @@ void decodedResponse() {
" }" +
"}";

this.client.post().bodyValue(JsonRequest.create(query))
.exchange()
.expectStatus().isOk()
.expectBody(new ParameterizedTypeReference<JsonResponse<Project>>() {})
.consumeWith(exchangeResult -> {
Project project = exchangeResult.getResponseBody().getDataEntry();
assertThat(project.getReleases()).hasSizeGreaterThan(1);
});
this.graphQLTester.query(query)
.execute()
.path("project")
.entity(Project.class)
.satisfies(project -> assertThat(project.getReleases()).hasSizeGreaterThan(1));
}

}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pluginManagement {
}

rootProject.name = 'spring-graphql'
include 'spring-graphql-web', 'graphql-spring-boot-starter', 'samples:webmvc-http', 'samples:webflux-websocket'
include 'spring-graphql-web', 'spring-graphql-test', 'graphql-spring-boot-starter', 'samples:webmvc-http', 'samples:webflux-websocket'
63 changes: 63 additions & 0 deletions spring-graphql-test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

plugins {
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java-library'
id 'maven'
}

description = "Spring Support for Testing GraphQL Applications"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

dependencyManagement {
imports {
mavenBom "com.fasterxml.jackson:jackson-bom:2.12.3"
mavenBom "io.projectreactor:reactor-bom:2020.0.6"
mavenBom "org.springframework:spring-framework-bom:5.3.6"
}
generatedPomCustomization {
enabled = false
}
}

dependencies {
api project(':spring-graphql-web')
api 'com.graphql-java:graphql-java:16.2'
api 'io.projectreactor:reactor-core'
api 'org.springframework:spring-context'
api 'org.springframework:spring-test'
api 'com.jayway.jsonpath:json-path:2.5.0'

compileOnly "javax.annotation:javax.annotation-api:1.3.2"
compileOnly 'org.springframework:spring-webflux'
compileOnly 'org.springframework:spring-webmvc'
compileOnly 'org.springframework:spring-websocket'
compileOnly 'javax.servlet:javax.servlet-api:4.0.1'
compileOnly 'org.skyscreamer:jsonassert:1.5.0'

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testImplementation 'org.assertj:assertj-core:3.19.0'
testImplementation 'org.mockito:mockito-core:3.8.0'
testImplementation 'org.skyscreamer:jsonassert:1.5.0'
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework:spring-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'io.projectreactor.netty:reactor-netty'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.14.9'
testImplementation 'com.fasterxml.jackson.core:jackson-databind'

testRuntime 'org.apache.logging.log4j:log4j-core:2.14.1'
testRuntime 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1'
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

apply from: "${rootDir}/gradle/publishing.gradle"
Loading