Skip to content

Commit 8c605cc

Browse files
committed
Merge pull request #49660 from ljrmorgan
* graphql_test_should_load_controller_advice: Polish "Consider ControllerAdvice with @graphqltest" Consider ControllerAdvice with @graphqltest Closes gh-49660
2 parents 8b70ee9 + af03b3b commit 8c605cc

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

spring-boot-project/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
@@ -426,7 +426,7 @@ There are javadoc:org.springframework.graphql.test.tester.GraphQlTester[] varian
426426

427427
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.
428428
javadoc:org.springframework.boot.test.autoconfigure.graphql.GraphQlTest[format=annotation] auto-configures the Spring GraphQL infrastructure, without any transport nor server being involved.
429-
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[].
429+
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[].
430430
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.
431431
javadoc:org.springframework.boot.context.properties.EnableConfigurationProperties[format=annotation] can be used to include javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans.
432432

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* relevant to GraphQL tests, including the following:
4848
* <ul>
4949
* <li>{@code @Controller}
50+
* <li>{@code @ControllerAdvice}
5051
* <li>{@code RuntimeWiringConfigurer}
5152
* <li>{@code @JsonComponent}
5253
* <li>{@code Converter}
@@ -123,10 +124,10 @@
123124
* Determines if default filtering should be used with
124125
* {@link SpringBootApplication @SpringBootApplication}. By default, only
125126
* {@code @Controller} (when no explicit {@link #controllers() controllers} are
126-
* defined), {@code RuntimeWiringConfigurer}, {@code @JsonComponent},
127-
* {@code Converter}, {@code GenericConverter}, {@code DataFetcherExceptionResolver},
128-
* {@code Instrumentation} and {@code GraphQlSourceBuilderCustomizer} beans are
129-
* included.
127+
* defined), {@code ControllerAdvice}, {@code RuntimeWiringConfigurer},
128+
* {@code @JsonComponent}, {@code Converter}, {@code GenericConverter},
129+
* {@code DataFetcherExceptionResolver}, {@code Instrumentation} and
130+
* {@code GraphQlSourceBuilderCustomizer} beans are included.
130131
* @see #includeFilters()
131132
* @see #excludeFilters()
132133
* @return if default filters should be used

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/graphql/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 @@ public class GraphQlTypeExcludeFilter extends StandardAnnotationCustomizableType
5152

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

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/graphql/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

@@ -59,6 +60,7 @@ void matchWhenHasNoControllers() throws Exception {
5960
GraphQlTypeExcludeFilter filter = new GraphQlTypeExcludeFilter(WithNoControllers.class);
6061
assertThat(excludes(filter, Controller1.class)).isFalse();
6162
assertThat(excludes(filter, Controller2.class)).isFalse();
63+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
6264
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
6365
assertThat(excludes(filter, ExampleService.class)).isTrue();
6466
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -74,6 +76,7 @@ void matchWhenHasController() throws Exception {
7476
GraphQlTypeExcludeFilter filter = new GraphQlTypeExcludeFilter(WithController.class);
7577
assertThat(excludes(filter, Controller1.class)).isFalse();
7678
assertThat(excludes(filter, Controller2.class)).isTrue();
79+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
7780
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
7881
assertThat(excludes(filter, ExampleService.class)).isTrue();
7982
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -89,6 +92,7 @@ void matchNotUsingDefaultFilters() throws Exception {
8992
GraphQlTypeExcludeFilter filter = new GraphQlTypeExcludeFilter(NotUsingDefaultFilters.class);
9093
assertThat(excludes(filter, Controller1.class)).isTrue();
9194
assertThat(excludes(filter, Controller2.class)).isTrue();
95+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isTrue();
9296
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isTrue();
9397
assertThat(excludes(filter, ExampleService.class)).isTrue();
9498
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -104,6 +108,7 @@ void matchWithIncludeFilter() throws Exception {
104108
GraphQlTypeExcludeFilter filter = new GraphQlTypeExcludeFilter(WithIncludeFilter.class);
105109
assertThat(excludes(filter, Controller1.class)).isFalse();
106110
assertThat(excludes(filter, Controller2.class)).isFalse();
111+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
107112
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
108113
assertThat(excludes(filter, ExampleService.class)).isTrue();
109114
assertThat(excludes(filter, ExampleRepository.class)).isFalse();
@@ -119,6 +124,7 @@ void matchWithExcludeFilter() throws Exception {
119124
GraphQlTypeExcludeFilter filter = new GraphQlTypeExcludeFilter(WithExcludeFilter.class);
120125
assertThat(excludes(filter, Controller1.class)).isTrue();
121126
assertThat(excludes(filter, Controller2.class)).isFalse();
127+
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
122128
assertThat(excludes(filter, ExampleRuntimeWiringConfigurer.class)).isFalse();
123129
assertThat(excludes(filter, ExampleService.class)).isTrue();
124130
assertThat(excludes(filter, ExampleRepository.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)