Consider ControllerAdvice with @GraphQlTest

This commit updates the GraphQL slice tests to also consider
`@ControllerAdvice`-annotated beans, since Spring GraphQL loads
`@GraphQlExceptionHandler`s declared in `@ControllerAdvice` beans.

This also aligns `@GraphQlTest` with `@WebMvcTest`.

See gh-49660

Signed-off-by: Louis Morgan <ljrmorgan@gmail.com>
This commit is contained in:
Louis Morgan
2026-03-20 10:02:07 +01:00
committed by Stéphane Nicoll
parent 8b70ee9dbc
commit 264a9aa43a
4 changed files with 19 additions and 5 deletions
@@ -426,7 +426,7 @@ There are javadoc:org.springframework.graphql.test.tester.GraphQlTester[] varian
Spring Boot helps you to test your {url-spring-graphql-docs}/controllers.html[Spring GraphQL Controllers] with the javadoc:org.springframework.boot.test.autoconfigure.graphql.GraphQlTest[format=annotation] annotation.
javadoc:org.springframework.boot.test.autoconfigure.graphql.GraphQlTest[format=annotation] auto-configures the Spring GraphQL infrastructure, without any transport nor server being involved.
This limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.graphql.execution.RuntimeWiringConfigurer[], javadoc:org.springframework.boot.jackson.JsonComponent[], javadoc:org.springframework.core.convert.converter.Converter[], javadoc:org.springframework.core.convert.converter.GenericConverter[], javadoc:org.springframework.graphql.execution.DataFetcherExceptionResolver[], javadoc:graphql.execution.instrumentation.Instrumentation[] and javadoc:org.springframework.boot.autoconfigure.graphql.GraphQlSourceBuilderCustomizer[].
This limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.web.bind.annotation.ControllerAdvice[format=annotation], javadoc:org.springframework.graphql.execution.RuntimeWiringConfigurer[], javadoc:org.springframework.boot.jackson.JsonComponent[], javadoc:org.springframework.core.convert.converter.Converter[], javadoc:org.springframework.core.convert.converter.GenericConverter[], javadoc:org.springframework.graphql.execution.DataFetcherExceptionResolver[], javadoc:graphql.execution.instrumentation.Instrumentation[] and javadoc:org.springframework.boot.autoconfigure.graphql.GraphQlSourceBuilderCustomizer[].
Regular javadoc:org.springframework.stereotype.Component[format=annotation] and javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans are not scanned when the javadoc:org.springframework.boot.test.autoconfigure.graphql.GraphQlTest[format=annotation] annotation is used.
javadoc:org.springframework.boot.context.properties.EnableConfigurationProperties[format=annotation] can be used to include javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans.
@@ -47,6 +47,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* relevant to GraphQL tests, including the following:
* <ul>
* <li>{@code @Controller}
* <li>{@code @ControllerAdvice}
* <li>{@code RuntimeWiringConfigurer}
* <li>{@code @JsonComponent}
* <li>{@code Converter}
@@ -123,10 +124,10 @@ public @interface GraphQlTest {
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default, only
* {@code @Controller} (when no explicit {@link #controllers() controllers} are
* defined), {@code RuntimeWiringConfigurer}, {@code @JsonComponent},
* {@code Converter}, {@code GenericConverter}, {@code DataFetcherExceptionResolver},
* {@code Instrumentation} and {@code GraphQlSourceBuilderCustomizer} beans are
* included.
* defined), {@code ControllerAdvice}, {@code RuntimeWiringConfigurer},
* {@code @JsonComponent}, {@code Converter}, {@code GenericConverter},
* {@code DataFetcherExceptionResolver}, {@code Instrumentation} and
* {@code GraphQlSourceBuilderCustomizer} beans are included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used
@@ -34,6 +34,7 @@ import org.springframework.graphql.execution.RuntimeWiringConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
/**
* {@link TypeExcludeFilter} for {@link GraphQlTest @GraphQlTest}.
@@ -51,6 +52,7 @@ public class GraphQlTypeExcludeFilter extends StandardAnnotationCustomizableType
static {
Set<Class<?>> includes = new LinkedHashSet<>();
includes.add(ControllerAdvice.class);
includes.add(JsonComponent.class);
includes.add(RuntimeWiringConfigurer.class);
includes.add(Converter.class);
@@ -42,6 +42,7 @@ import org.springframework.graphql.server.WebGraphQlResponse;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import static org.assertj.core.api.Assertions.assertThat;
@@ -60,6 +61,7 @@ class GraphQlTypeExcludeFilterTests {
assertThat(excludes(filter, Controller1.class)).isFalse();
assertThat(excludes(filter, Controller2.class)).isFalse();
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
assertThat(excludes(filter, ExampleService.class)).isTrue();
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -75,6 +77,7 @@ class GraphQlTypeExcludeFilterTests {
assertThat(excludes(filter, Controller1.class)).isFalse();
assertThat(excludes(filter, Controller2.class)).isTrue();
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
assertThat(excludes(filter, ExampleService.class)).isTrue();
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -90,6 +93,7 @@ class GraphQlTypeExcludeFilterTests {
assertThat(excludes(filter, Controller1.class)).isTrue();
assertThat(excludes(filter, Controller2.class)).isTrue();
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isTrue();
assertThat(excludes(filter, ExampleControllerAdvice.class)).isTrue();
assertThat(excludes(filter, ExampleService.class)).isTrue();
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -105,6 +109,7 @@ class GraphQlTypeExcludeFilterTests {
assertThat(excludes(filter, Controller1.class)).isFalse();
assertThat(excludes(filter, Controller2.class)).isFalse();
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
assertThat(excludes(filter, ExampleService.class)).isTrue();
assertThat(excludes(filter, ExampleRepository.class)).isFalse();
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -120,6 +125,7 @@ class GraphQlTypeExcludeFilterTests {
assertThat(excludes(filter, Controller1.class)).isTrue();
assertThat(excludes(filter, Controller2.class)).isFalse();
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
assertThat(excludes(filter, ExampleService.class)).isTrue();
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -169,6 +175,11 @@ class GraphQlTypeExcludeFilterTests {
}
@ControllerAdvice
static class ExampleControllerAdvice {
}
@Service
static class ExampleService {