Skip to content

Commit b4eec55

Browse files
committed
@GraphQlTest should load @ControllerAdvice
GraphQL slice tests should load `@ControllerAdvice` beans, since Spring GraphQL will load `@GraphQlExceptionHandler`s declared in `@ControllerAdvice` beans. This aligns `@GraphQlTest` with `@WebMvcTest`, which does load `@ControllerAdvice` beans. Signed-off-by: Louis Morgan <ljrmorgan@gmail.com>
1 parent 8bcb7fe commit b4eec55

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/spring-boot-applications.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ There are javadoc:org.springframework.graphql.test.tester.GraphQlTester[] varian
458458

459459
Spring Boot helps you to test your {url-spring-graphql-docs}/controllers.html[Spring GraphQL Controllers] with the javadoc:org.springframework.boot.graphql.test.autoconfigure.GraphQlTest[format=annotation] annotation from the `spring-boot-graphql-test` module.
460460
javadoc:org.springframework.boot.graphql.test.autoconfigure.GraphQlTest[format=annotation] auto-configures the Spring GraphQL infrastructure, without any transport nor server being involved.
461-
This limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.graphql.execution.RuntimeWiringConfigurer[], javadoc:org.springframework.boot.jackson.JacksonComponent[], javadoc:org.springframework.boot.jackson2.JsonComponent[format=annotation] (deprecated), 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.graphql.autoconfigure.GraphQlSourceBuilderCustomizer[].
461+
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.JacksonComponent[], javadoc:org.springframework.boot.jackson2.JsonComponent[format=annotation] (deprecated), 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.graphql.autoconfigure.GraphQlSourceBuilderCustomizer[].
462462
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.graphql.test.autoconfigure.GraphQlTest[format=annotation] annotation is used.
463463
javadoc:org.springframework.boot.context.properties.EnableConfigurationProperties[format=annotation] can be used to include javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans.
464464

module/spring-boot-graphql-test/src/main/java/org/springframework/boot/graphql/test/autoconfigure/GraphQlTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* relevant to GraphQL tests, including the following:
4747
* <ul>
4848
* <li>{@code @Controller}
49+
* <li>{@code @ControllerAdvice}
4950
* <li>{@code RuntimeWiringConfigurer}
5051
* <li>{@code @JacksonComponent}
5152
* <li>{@code @JsonComponent} (deprecated)
@@ -122,10 +123,11 @@
122123
* Determines if default filtering should be used with
123124
* {@link SpringBootApplication @SpringBootApplication}. By default, only
124125
* {@code @Controller} (when no explicit {@link #controllers() controllers} are
125-
* defined), {@code RuntimeWiringConfigurer}, {@code @JacksonComponent},
126-
* {@code @JsonComponent} (deprecated), {@code Converter}, {@code GenericConverter},
127-
* {@code DataFetcherExceptionResolver}, {@code Instrumentation} and
128-
* {@code GraphQlSourceBuilderCustomizer} beans are included.
126+
* defined), {@code ControllerAdvice}, {@code RuntimeWiringConfigurer},
127+
* {@code @JacksonComponent}, {@code @JsonComponent} (deprecated), {@code Converter},
128+
* {@code GenericConverter}, {@code DataFetcherExceptionResolver},
129+
* {@code Instrumentation} and {@code GraphQlSourceBuilderCustomizer} beans are
130+
* included.
129131
* @see #includeFilters()
130132
* @see #excludeFilters()
131133
* @return if default filters should be used

module/spring-boot-graphql-test/src/main/java/org/springframework/boot/graphql/test/autoconfigure/GraphQlTypeExcludeFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.stereotype.Controller;
3535
import org.springframework.util.ClassUtils;
3636
import org.springframework.util.ObjectUtils;
37+
import org.springframework.web.bind.annotation.ControllerAdvice;
3738

3839
/**
3940
* {@link TypeExcludeFilter} for {@link GraphQlTest @GraphQlTest}.
@@ -51,6 +52,7 @@ class GraphQlTypeExcludeFilter extends StandardAnnotationCustomizableTypeExclude
5152

5253
static {
5354
Set<Class<?>> includes = new LinkedHashSet<>();
55+
includes.add(ControllerAdvice.class);
5456
includes.add(JacksonComponent.class);
5557
includes.add(RuntimeWiringConfigurer.class);
5658
includes.add(Converter.class);

module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/GraphQlTypeExcludeFilterTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.stereotype.Controller;
4343
import org.springframework.stereotype.Repository;
4444
import org.springframework.stereotype.Service;
45+
import org.springframework.web.bind.annotation.ControllerAdvice;
4546

4647
import static org.assertj.core.api.Assertions.assertThat;
4748

@@ -60,6 +61,7 @@ void matchWhenHasNoControllers() throws Exception {
6061
assertThat(excludes(filter, Controller1.class)).isFalse();
6162
assertThat(excludes(filter, Controller2.class)).isFalse();
6263
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
64+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
6365
assertThat(excludes(filter, ExampleService.class)).isTrue();
6466
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
6567
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -75,6 +77,7 @@ void matchWhenHasController() throws Exception {
7577
assertThat(excludes(filter, Controller1.class)).isFalse();
7678
assertThat(excludes(filter, Controller2.class)).isTrue();
7779
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
80+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
7881
assertThat(excludes(filter, ExampleService.class)).isTrue();
7982
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
8083
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -90,6 +93,7 @@ void matchNotUsingDefaultFilters() throws Exception {
9093
assertThat(excludes(filter, Controller1.class)).isTrue();
9194
assertThat(excludes(filter, Controller2.class)).isTrue();
9295
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isTrue();
96+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isTrue();
9397
assertThat(excludes(filter, ExampleService.class)).isTrue();
9498
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
9599
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -105,6 +109,7 @@ void matchWithIncludeFilter() throws Exception {
105109
assertThat(excludes(filter, Controller1.class)).isFalse();
106110
assertThat(excludes(filter, Controller2.class)).isFalse();
107111
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
112+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
108113
assertThat(excludes(filter, ExampleService.class)).isTrue();
109114
assertThat(excludes(filter, ExampleRepository.class)).isFalse();
110115
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -120,6 +125,7 @@ void matchWithExcludeFilter() throws Exception {
120125
assertThat(excludes(filter, Controller1.class)).isTrue();
121126
assertThat(excludes(filter, Controller2.class)).isFalse();
122127
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
128+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
123129
assertThat(excludes(filter, ExampleService.class)).isTrue();
124130
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
125131
assertThat(excludes(filter, ExampleWebInterceptor.class)).isTrue();
@@ -169,6 +175,11 @@ static class Controller2 {
169175

170176
}
171177

178+
@ControllerAdvice
179+
static class ExampleControllerAdvice {
180+
181+
}
182+
172183
@Service
173184
static class ExampleService {
174185

0 commit comments

Comments
 (0)