Can’t autowire GraphQlTester

Can’t autowire GraphQlTester


1

My current dependency set-up for demo api:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-graphql:2.7.2-SNAPSHOT'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.7.2-SNAPSHOT'
    testImplementation 'org.springframework:spring-webflux'
    testImplementation 'org.springframework.graphql:spring-graphql-test:1.0.0'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
}

Test class:

import com.example.demo.dao.ProfileDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.graphql.test.tester.GraphQlTester;

import static org.springframework.test.util.AssertionErrors.assertNotNull;

@GraphQlTest
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class DemoApplicationTests {

    @Autowired
    GraphQlTester graphQlTester;
    @MockBean
    ProfileDao dao;

    @Test
    void contextLoads() {
        assertNotNull("graphQlTester null", graphQlTester);
    }

}

I’ve tried few approaches from varios advices from internet like using SpringBootTest instead of GraphQlTest but nothing worked.

1

  • From extended logs: GraphQlAutoConfiguration: Did not match: – @ ConditionalOnGraphQlSchema did not find schema files in locations 'classpath:graphql/**/'; @ ConditionalOnGraphQlSchema did not find GraphQlSourceBuilderCustomizer (DefaultGraphQlSchemaCondition) and as the result GraphQlTesterAutoConfiguration#graphQlTester: Did not match: – @ ConditionalOnBean (types: org.springframework.graphql.ExecutionGraphQlService; SearchStrategy: all) did not find any beans of type org.springframework.graphql.ExecutionGraphQlService (OnBeanCondition)

    – Slongtong

    Jul 8, 2022 at 13:10


1 Answer
1


0

here’s how I fixed it:

You have to use Java 11 with your spring project, and select the 1.0.0 version of graphql-test. Else, you need at least java 17 for your project and the latest version of graphql-test.

  • For Java 11:
<properties>
  <java.version>11</java.version>
  <graphql.test.version>1.0.0</graphql.test.version>
</properties>

<!-- Dependencies -->
<!-- GraphQL -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.graphql</groupId>
  <artifactId>spring-graphql-test</artifactId>
  <version>${graphql.test.version}</version>
  <scope>test</scope>
</dependency>
  • For Java 17+:
<properties>
  <java.version>17</java.version>
  <graphql.test.version>1.2.3</graphql.test.version>
</properties>

<!-- Dependencies -->
<!-- GraphQL -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.graphql</groupId>
  <artifactId>spring-graphql-test</artifactId>
  <version>${graphql.test.version}</version>
  <scope>test</scope>
</dependency>



Leave a Reply

Your email address will not be published. Required fields are marked *