I have a Spring Boot application that uses GraphQL. I would like to add integration testing using Spring Boot GraphQL Test library. I am trying the following test:
@GraphQLTest
public class UserTest {
@Autowired
private GraphQLTestTemplate graphQLTestTemplate;
@Test
public void createUser() throws IOException {
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/register.graphql");
assertNotNull(response);
assertTrue(response.isOk());
assertEquals("1", response.get("$.data.post.id"));
}
}
and I get the following response:
<404,<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> The requested resource [/api/graphql] is not available</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.37</h3></body></html>,[Content-Type:"text/html;charset=utf-8", Content-Language:"en", Content-Length:"766", Date:"Sun, 08 Nov 2020 00:18:11 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
Here is the related GitHub issue: https://github.com/graphql-java-kickstart/graphql-spring-boot/issues/267
Any help is appreciated ...
I had the same issue. Remove the @GraphQLTest
and add the below one. I am not sure why this worked although the GraphQLTest
interface has the @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserTest {
@Autowired
private GraphQLTestTemplate graphQLTestTemplate;
@Test
public void createUser() throws IOException {
GraphQLResponse response = graphQLTestTemplate.postForResource("graphql/register.graphql");
assertNotNull(response);
assertTrue(response.isOk());
assertEquals("1", response.get("$.data.post.id"));
}
}