SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureGraphQlTester
class SpaceControllerTest {
@Autowired
private GraphQlTester graphQlTester;
// below the test cases`
Throwing exception : Unsatisfied dependency expressed through field ‘graphQlTester’: No qualifying bean of type ‘org.springframework.graphql.test.tester.GraphQlTester’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I tried working on various options but not getting it injected in Sprinboot3.x .
Any link to guide how to make it work with Springboot 3.x would help. Thankyou`
1 Answer
I also ran into the same problem with Spring Boot 3. I found another way to test the controller in one of the code repositories on github. Instead of using GraphQlTester I have used HttpGraphQlTester. Here is my sample controller
package com.ms.springgraphql.controller;
import com.ms.springgraphql.model.Book;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.graphql.test.tester.HttpGraphQlTester;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class BookControllerTest {
private HttpGraphQlTester graphQlTester;
@LocalServerPort
int port;
@BeforeEach
void setUp() {
WebTestClient client = WebTestClient.bindToServer()
.baseUrl(String.format("https://localhost:%s/graphql", port))
.build();
graphQlTester = HttpGraphQlTester.create(client);
}
@Test
void contextLoads() {
assertNotNull(graphQlTester);
}
@Test
void shouldReturnBook(){
//given
// language=GraphQL
String document = """
query {
findOne(id: 1){
pages
rating
title
}
}
""";
//when
graphQlTester.document(document)
.variable("id", 1)
.execute()
.path("findOne")
.entity(Book.class)
.satisfies(book -> {
assertEquals("The Great Novel", book.getTitle());
assertEquals(300,book.getPages());
});
}
}