question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integration Test fails, /graphql enpoint not found

See original GitHub issue

If i try to execute an integration test the graphql endpoint is not found, the test returns a “404”

RunWith(SpringRunner.class)
@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@EnableConfigurationProperties(GraphQLServletProperties.class)
@TestPropertySource(
        locations = "classpath:application-integrationtest.yaml")
public class GraphQlControllerTest {


    @Autowired
    private MockMvc mvc;


    @Test
    public void myTests() throws Exception {
        String request = "{\"query\": \"query{groups{id}}\", \"variables\": null}";
        ResultActions response = mvc.perform(post("/graphql").content(request).contentType(MediaType.APPLICATION_JSON));

        response.andExpect(status().isOk());
    }
}

output of the test is:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /graphql
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status 
Expected :200
Actual   :404

has anyone an working example for writing integration tests?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Sam-Kruglovcommented, Jan 7, 2020

I came to the conclusion that MockMvc is not supposed to be used for testing raw servlets, as here we are not exposing /graphql endpoint as a Spring MVC Controller, but rather as a raw HttpServlet.

The following works for me quite nicely:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class AbstractGraphQLTest {

    @SpringBootApplication
    static class App {

    }

    @Autowired
    private WebTestClient webTestClient;

    @Autowired
    private ObjectMapper mapper;

    private String json(String query) throws JsonProcessingException {
        ObjectNode node = mapper.createObjectNode();
        node.put("query", query);
        return mapper.writeValueAsString(node);
    }

    protected WebTestClient.BodyContentSpec graphql(String query) throws JsonProcessingException {
        return webTestClient.post()
                .uri("/graphql")
                .bodyValue(json(query))
                .exchange()
                .expectBody();
    }

}

and then:

class default_handler extends AbstractGraphQLTest {

        @Test
        void expected_error__message_from_error() throws IOException {
            graphql("{illegalArgumentException}")
                    .jsonPath("errors[0].message").isEqualTo(EXPECTED_ERROR_MESSAGE)
                    .jsonPath("data.illegalArgumentException").doesNotExist();
        }
}
1reaction
Thinkenterprisecommented, Jan 4, 2019

I also have problems with the test. I also get the error 404 in the GraphQLResult. The loading of the test query getAllRoutes.graphql works, that I have checked. I think the problem is that the application will not start up, because the spring beans will not be created. I have set a few breakpoints that are not called.

The https://github.com/Thinkenterprise/spring-boot-graphql

Here is the excerpt from my test.

@RunWith(SpringRunner.class)
@GraphQLTest
public class GraphQLRouteTest {

   @Autowired
    private GraphQLTestTemplate graphQLTestTemplate;

    @Test
    public void routes() throws IOException {
        GraphQLResponse response = graphQLTestTemplate.postForResource("getAllRoutes.graphql");
        assertNotNull(response);
        assertTrue(response.isOk());
        assertEquals("1", response.get("$.data.routes.id"));
    }

}

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL Test returns 404 not found - Stack Overflow
I am not sure why this worked although the GraphQLTest interface has the @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.
Read more >
Integration Test fails, /graphql enpoint not found - Bountysource
If i try to execute an integration test the graphql endpoint is not found, the test returns a "404" RunWith(SpringRunner.class) ...
Read more >
Full Stack Error Handling with GraphQL and Apollo
If networkError is present in your response, it means your entire query was rejected, and therefore no data was returned. For example, the ......
Read more >
Debug "Resource not accessible by integration" error when ...
This error basically talks about a permission issue. When working with Github apps, you may not have set the necessary permission to access...
Read more >
Testing your GraphQL APIs in a Spring-boot app - Medium
If it's not already done, create this directory in your project: src/test/resources/graphql then create these 3 request file in it. create-user.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found