0

Error: Cannot invoke "org.springframework.graphql.test.tester.GraphQlTester.document(String)" because "this.graphQlTester" is null

Getting the above error. Have checked the jar files and they exist. But still can not find the autowiring being done in logs (with debug level in root).

package com.reports.controller;

import com.reports.entity.ReportDetails;
import com.reports.service.ReportDetailsService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.context.annotation.Import;
import org.springframework.graphql.test.tester.GraphQlTester;

@Import(ReportDetailsService.class)
@GraphQlTest(ReportsController.class)
public class ReportsControllerIntTest {
    @Autowired
    GraphQlTester graphQlTester;

    @Test
    public void testReadAllReportsShouldReturnAllReports() throws Exception {
        String query = """
            query {
                readAll {
                    id
                    reportName
                    columns
                }
            }
        """;

    graphQlTester.document(query)
        .execute()
        .path("readAll")
        .entityList(ReportDetails.class)
        .hasSizeGreaterThan(1);

    }
}
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
Shourya Sharma
  • 547
  • 4
  • 23

2 Answers2

1

Quoting M.Deinum from the comments:

Wrong @Test annotation, should use the one from org.junit.jupiter.api. Now it runs with JUnit4 while the @GraphQLTest is by default annoated for JUnit5. So either switch or add @RunWith(SpringRunner.class) to make it for JUnit4. I do recommend to work with JUnit5.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
-1

I think you forget @AutoConfigureGraphQlTester. This example works for me:

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.graphql.tester.AutoConfigureGraphQlTester;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.graphql.test.tester.GraphQlTester;
import org.springframework.test.annotation.DirtiesContext;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
@AutoConfigureGraphQlTester
class UserControllerTest {

  @Autowired GraphQlTester graphQlTester;

  @BeforeEach
  void setUp() {
    createUser(new User("gurkan", "gurkan@mail.com", Role.ADMIN));
    createUser(new User("ahmet", "ahmet@mail.com", Role.USER));
  }

  @Test
  void when_getAllUsers_should_return_userList() {

    // language=graphql
    String query =
        """
        query {
          getAllUsers{
            id
            username
            role
            created
            updated
          }
        }
        """;

    graphQlTester.document(query).execute().path("getAllUsers").entityList(User.class).hasSize(2);
  }
gurkan
  • 509
  • 1
  • 4
  • 18
  • He didn't, as you can see in the [`@GraphQLTest`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/graphql/GraphQlTest.html) that is already included. So adding that instead of `@GraphQL` will only make things worse (as it does less). – M. Deinum Apr 26 '23 at 08:52