I believe this is a feature request, as I did not see any facilities in the current spring-graphql-test library to accomplish this.

When testing the Controller layer of our application using GraphQlTester, it would be nice to be able to configure the inclusion of our custom DataFetcherExceptionResolver to validate the error handling capabilities we have configured and to assert the ErrorType / error maps in the response.

Comment From: bclozel

How are you setting things up with your test? Are you manually wiring things? Are you doing a full integration test with a live server? Are you using Spring Boot with @GraphQlTest? Providing a code snippet showing your test class and the related setup should help here.

Comment From: JimHosken

@bclozel I am using @GraphQlTest and autowiring @GraphQlTester into my class. I was trying to test my custom subclass of DataFetcherExceptionResolverAdapter by throwing an exception from my mock service layer in one of these tests (not depicted below). It doesn't appear the DataFetcherExceptionResolver is ever created.

Here is a snippet below:

`

@ExtendWith(SpringExtension.class)
@GraphQlTest(NotesQueryController.class)
public class NotesQueryControllerTest {

private static final String CLAIM_NUMBER = "UNITTEST00001";
private static final String CLAIM_NOTE_BODY = "testClaimNoteBody";

@Autowired
private GraphQlTester graphQlTester;

@MockBean
ClaimNoteService claimNoteService;

@Captor
ArgumentCaptor<String> claimNumberCaptor;

private static final String QUERY_FETCHALLNOTES = "query($claimNumber: String!) {\n" +
        "    fetchAllNotes(claimNumber: $claimNumber) {\n" +
        ...
        "    }\n" +
        "}";

private static final String QUERY_FETCHMOSTRECENTPERCONTENTTYPE = "query($claimNumber: String!) {\n" +
        "    fetchMostRecentNotePerContentType(claimNumber: $claimNumber) {\n" +
       ...
        "    }\n" +
        "}\n";

@Test
public void fetchAllClaimNotes() throws Exception {

    when(claimNoteService.fetchAllNotes(claimNumberCaptor.capture(), isNull(), isNull()))
        .thenReturn(ClaimNoteFixture.claimNoteInfo(CLAIM_NUMBER));

    GraphQlTester.ResponseSpec response = graphQlTester.query(QUERY_FETCHALLNOTES)
        .variable("claimNumber", CLAIM_NUMBER)
        .execute();

    response.path("fetchAllNotes.noteCount")
                .entity(Integer.class).isEqualTo(1)
            .path("fetchAllNotes.notes[0].claimInfo.claimNumber")
                .entity(String.class).isEqualTo(CLAIM_NUMBER)
            .path("fetchAllNotes.notes[0].body")
                .entity(String.class).isEqualTo(CLAIM_NOTE_BODY);

    assertThat(claimNumberCaptor.getValue()).isEqualTo(CLAIM_NUMBER);

    verify(claimNoteService).fetchAllNotes(eq(CLAIM_NUMBER), isNull(), isNull());
    verifyNoMoreInteractions(claimNoteService);
}

@Test
public void fetchMostRecentClaimNotesByContentType() {
    when(claimNoteService.fetchMostRecentNotePerContentTypeByClaimNumber(CLAIM_NUMBER))
            .thenReturn(ClaimNoteFixture.createClaimNote(CLAIM_NUMBER));

    GraphQlTester.ResponseSpec response = graphQlTester.query(QUERY_FETCHMOSTRECENTPERCONTENTTYPE)
            .variable("claimNumber", CLAIM_NUMBER)
            .execute();

    response.path("fetchMostRecentNotePerContentType[0].claimInfo.claimNumber")
                .entity(String.class).isEqualTo(CLAIM_NUMBER)
            .path("fetchMostRecentNotePerContentType[0].body")
                .entity(String.class).isEqualTo(CLAIM_NOTE_BODY);
}

... }`

Comment From: bclozel

Thanks @JimHosken this is a bug in the Spring Boot ˋ@GraphQlTest` support. We are not including all the required types in this sliced test.

Comment From: bclozel

Hi, this is a first-timers-only issue. This means we've worked to make it more legible to folks who either haven't contributed to our codebase before, or even folks who haven't contributed to open source before.

If that's you, we're interested in helping you take the first step and can answer questions and help you out as you do. Note that we're especially interested in contributions from people from groups underrepresented in free and open source software!

If you have contributed before, consider leaving this one for someone new, and looking through our general ideal-for-contribution issues. Thanks!

Problem

Spring Boot helps you to write integration tests without involving the entire application context (all beans, live servers, etc.). These "test slices" are supported with annotations that are specific to the "slice" we'd like to test. For Spring GraphQL applications, we now provide a @GraphQlTest slice.

Part of the test slice support is about filtering what kind of beans we should consider when scanning - rejecting the types that are out of scope for this slice and only keeping the relevant ones for what we're testing. For that, we a GraphQlTypeExcludeFilter imported by the test annotation itself.

The problem here is that our GraphQlTypeExcludeFilter currently allows JsonComponent, RuntimeWiringConfigurer, Converter.class, GenericConverter beans only. The issue reported here shows that other beans should be considered.

We should also include DataFetcherExceptionResolver, Instrumentation and GraphQlSourceBuilderCustomizer as they are used to set up the GraphQL source.

Solution

The GraphQlTypeExcludeFilter should be updated to also allow those types (i.e. not filter them out). Tests should be added in GraphQlTypeExcludeFilterTests. This should be very similar to how RuntimeWiringConfigurer components are tested in this class.

The list of allowed types is also written in the reference documentation. You should also update the reference docs accordingly. You'll find the file here spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc in your local copy.

Important: because Spring GraphQL support was added in 2.7.x (and is not currently supported in 3.0.x), you need to checkout the 2.7.x branch and submit a pull request against it.

Steps to Fix

  • [ ] Claim this issue with a comment below and ask any clarifying questions you need
  • [ ] Set up a repository locally following the Contributing Guidelines
  • [ ] Try to fix the issue following the steps above
  • [ ] Commit your changes and start a pull request.

Comment From: GauthamM-official

@bclozel Hi, Shall I work on this?

Comment From: bclozel

It doesn't seem you've contributed in the past here; if that's the case, this issue is all yours! Don't hesitate to ask for clarifications here, we're here to help.

Comment From: GauthamM-official

Thank you @bclozel. I will post any questions/doubts in this thread.

Comment From: GauthamM-official

@bclozel I see that a LinkedHashSet is being used. So is there any specific order in which DataFetcherExceptionResolver, Instrumentation and GraphQlSourceBuilderCustomizer are to be added to the set?

Comment From: bclozel

No, there is no particular order.

Comment From: GauthamM-official

@bclozel Shouldn't the javadoc for @GraphQlTest also be updated?

Comment From: bclozel

Yes they should. Sorry I forgot to mention that.

Comment From: GauthamM-official

@bclozel Added PR, please review.

Comment From: bclozel

Cool, thanks @GauthamM-official ! Closing this issue in favor of #30078