Fix for GraphQL testing

You are here because you are following the “Building a GraphQL service“, and specifically the testing section using GraphQlTest, and you keep getting a runtime error:

java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@5b202ff testClass = com.tiemens.fullstack.controller.BookControllerTests, locations = [], classes = [com.tiemens.fullstack.FullstackApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.graphql.GraphQlTestContextBootstrapper=true"]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphQlSource' defined in class path resource [org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.class]: Failed to instantiate [org.springframework.graphql.execution.GraphQlSource]: Factory method 'graphQlSource' threw exception with message: Name for argument of type [java.lang.String] not specified, and parameter name information not found in class file either.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.graphql.execution.GraphQlSource]: Factory method 'graphQlSource' threw exception with message: Name for argument of type [java.lang.String] not specified, and parameter name information not found in class file either.
Caused by: java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not found in class file either.

The fix is in BookController, bookById: simply change “@Argument String id” to be “@Argument(“id”) String id”, as shown below:

@Controller
public class BookController {
    @QueryMapping
    public Book bookById(@Argument("id") String id) {
        return Book.getById(id);
    }
    ...etc...
}

Magically injected error that is magically fixed.

Note: this was caused by Spring Boot v3,4,1 (or 3.4, or 3.2 or something in the “3”s)

This entry was posted in Software Engineering. Bookmark the permalink.