Skip to content

Commit 9a46328

Browse files
committed
Restore support for Jackson 2 in slice tests
This commit restores the support of Jackson 2 in @WebMvcTest, @WebFluxTest, @restclienttest, and @graphqltest. Closes gh-47869
1 parent 665c4ac commit 9a46328

File tree

25 files changed

+331
-20
lines changed

25 files changed

+331
-20
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ To test that object JSON serialization and deserialization is working as expecte
296296
javadoc:org.springframework.boot.json.test.autoconfigure.JsonTest[format=annotation] auto-configures the available supported JSON mapper, which can be one of the following libraries:
297297

298298
* Jackson javadoc:tools.jackson.databind.JsonMapper[], any javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation] beans and any Jackson javadoc:tools.jackson.databind.JacksonModule[]
299+
* Jackson 2 (deprecated) javadoc:com.fasterxml.jackson.databind.ObjectMapper[], any javadoc:org.springframework.boot.jackson2.JsonComponent[format=annotation] beans and any Jackson javadoc:com.fasterxml.jackson.databind.Module[]
299300
* `Gson`
300301
* `Jsonb`
301302

@@ -325,7 +326,7 @@ include-code::MyJsonAssertJTests[tag=*]
325326
== Auto-configured Spring MVC Tests
326327

327328
To test whether Spring MVC controllers are working as expected, use the javadoc:org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest[format=annotation] annotation from the `spring-boot-webmvc-test` module.
328-
javadoc:org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest[format=annotation] auto-configures the Spring MVC infrastructure and limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.web.bind.annotation.ControllerAdvice[format=annotation], javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation], javadoc:org.springframework.core.convert.converter.Converter[], javadoc:org.springframework.core.convert.converter.GenericConverter[], javadoc:jakarta.servlet.Filter[], javadoc:org.springframework.web.servlet.HandlerInterceptor[], javadoc:org.springframework.web.servlet.config.annotation.WebMvcConfigurer[], javadoc:org.springframework.boot.webmvc.autoconfigure.WebMvcRegistrations[], and javadoc:org.springframework.web.method.support.HandlerMethodArgumentResolver[].
329+
javadoc:org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest[format=annotation] auto-configures the Spring MVC infrastructure and limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.web.bind.annotation.ControllerAdvice[format=annotation], javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation], javadoc:org.springframework.boot.jackson2.JsonComponent[format=annotation] (deprecated), javadoc:org.springframework.core.convert.converter.Converter[], javadoc:org.springframework.core.convert.converter.GenericConverter[], javadoc:jakarta.servlet.Filter[], javadoc:org.springframework.web.servlet.HandlerInterceptor[], javadoc:org.springframework.web.servlet.config.annotation.WebMvcConfigurer[], javadoc:org.springframework.boot.webmvc.autoconfigure.WebMvcRegistrations[], and javadoc:org.springframework.web.method.support.HandlerMethodArgumentResolver[].
329330
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.webmvc.test.autoconfigure.WebMvcTest[format=annotation] annotation is used.
330331
javadoc:org.springframework.boot.context.properties.EnableConfigurationProperties[format=annotation] can be used to include javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans.
331332

@@ -369,7 +370,7 @@ TIP: Sometimes writing Spring MVC tests is not enough; Spring Boot can help you
369370
== Auto-configured Spring WebFlux Tests
370371

371372
To test that {url-spring-framework-docs}/web-reactive.html[Spring WebFlux] controllers are working as expected, you can use the javadoc:org.springframework.boot.webflux.test.autoconfigure.WebFluxTest[format=annotation] annotation from the `spring-boot-webflux-test` module.
372-
javadoc:org.springframework.boot.webflux.test.autoconfigure.WebFluxTest[format=annotation] auto-configures the Spring WebFlux infrastructure and limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.web.bind.annotation.ControllerAdvice[format=annotation], javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation], javadoc:org.springframework.core.convert.converter.Converter[], javadoc:org.springframework.core.convert.converter.GenericConverter[] and javadoc:org.springframework.web.reactive.config.WebFluxConfigurer[].
373+
javadoc:org.springframework.boot.webflux.test.autoconfigure.WebFluxTest[format=annotation] auto-configures the Spring WebFlux infrastructure and limits scanned beans to javadoc:org.springframework.stereotype.Controller[format=annotation], javadoc:org.springframework.web.bind.annotation.ControllerAdvice[format=annotation], javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation], javadoc:org.springframework.boot.jackson2.JsonComponent[format=annotation] (deprecated), javadoc:org.springframework.core.convert.converter.Converter[], javadoc:org.springframework.core.convert.converter.GenericConverter[] and javadoc:org.springframework.web.reactive.config.WebFluxConfigurer[].
373374
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.webflux.test.autoconfigure.WebFluxTest[format=annotation] annotation is used.
374375
javadoc:org.springframework.boot.context.properties.EnableConfigurationProperties[format=annotation] can be used to include javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans.
375376

@@ -440,7 +441,7 @@ There are javadoc:org.springframework.graphql.test.tester.GraphQlTester[] varian
440441

441442
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.
442443
javadoc:org.springframework.boot.graphql.test.autoconfigure.GraphQlTest[format=annotation] auto-configures the Spring GraphQL infrastructure, without any transport nor server being involved.
443-
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.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[].
444+
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[].
444445
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.
445446
javadoc:org.springframework.boot.context.properties.EnableConfigurationProperties[format=annotation] can be used to include javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation] beans.
446447

module/spring-boot-graphql-test/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
implementation(project(":module:spring-boot-jackson"))
3232

3333
optional(project(":core:spring-boot-autoconfigure"))
34+
optional(project(":module:spring-boot-jackson2"))
3435
optional(project(":module:spring-boot-validation"))
3536
optional(project(":module:spring-boot-webclient"))
3637
optional(project(":module:spring-boot-webmvc-test"))

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* <li>{@code @Controller}
4949
* <li>{@code RuntimeWiringConfigurer}
5050
* <li>{@code @JacksonComponent}
51+
* <li>{@code @JsonComponent} (deprecated)
5152
* <li>{@code Converter}
5253
* <li>{@code GenericConverter}
5354
* <li>{@code DataFetcherExceptionResolver}
@@ -122,9 +123,9 @@
122123
* {@link SpringBootApplication @SpringBootApplication}. By default, only
123124
* {@code @Controller} (when no explicit {@link #controllers() controllers} are
124125
* defined), {@code RuntimeWiringConfigurer}, {@code @JacksonComponent},
125-
* {@code Converter}, {@code GenericConverter}, {@code DataFetcherExceptionResolver},
126-
* {@code Instrumentation} and {@code GraphQlSourceBuilderCustomizer} beans are
127-
* included.
126+
* {@code @JsonComponent} (deprecated), {@code Converter}, {@code GenericConverter},
127+
* {@code DataFetcherExceptionResolver}, {@code Instrumentation} and
128+
* {@code GraphQlSourceBuilderCustomizer} beans are included.
128129
* @see #includeFilters()
129130
* @see #excludeFilters()
130131
* @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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class GraphQlTypeExcludeFilter extends StandardAnnotationCustomizableTypeExclude
4444

4545
private static final Class<?>[] NO_CONTROLLERS = {};
4646

47-
private static final String[] OPTIONAL_INCLUDES = { "tools.jackson.databind.JacksonModule" };
47+
private static final String[] OPTIONAL_INCLUDES = { "tools.jackson.databind.JacksonModule",
48+
"com.fasterxml.jackson.databind.Module", "org.springframework.boot.jackson2.JsonComponent" };
4849

4950
private static final Set<Class<?>> KNOWN_INCLUDES;
5051

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain
197197

198198
}
199199

200-
@SuppressWarnings("serial")
201200
static class ExampleModule extends SimpleModule {
202201

203202
}

module/spring-boot-restclient-test/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929

3030
optional(project(":core:spring-boot-autoconfigure"))
3131
optional(project(":module:spring-boot-jackson"))
32+
optional(project(":module:spring-boot-jackson2"))
3233
optional(project(":module:spring-boot-restclient"))
3334
optional("org.apache.httpcomponents.client5:httpclient5")
3435
optional("org.junit.jupiter:junit-jupiter-api")

module/spring-boot-restclient-test/src/main/java/org/springframework/boot/restclient/test/autoconfigure/RestClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@
4646
* tests. Similarly, component scanning is limited to beans annotated with:
4747
* <ul>
4848
* <li>{@code @JacksonComponent}</li>
49+
* <li>{@code @JsonComponent}(deprecated)</li>
4950
* </ul>
5051
* <p>
5152
* as well as beans that implement:
5253
* <ul>
5354
* <li>{@code JacksonModule}, if Jackson is available</li>
55+
* <li>{@code Module}, if Jackson 2 is available (deprecated)</li>
5456
* </ul>
5557
* <p>
5658
* By default, tests annotated with {@code RestClientTest} will also auto-configure a

module/spring-boot-restclient-test/src/main/java/org/springframework/boot/restclient/test/autoconfigure/RestClientTypeExcludeFilter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Set;
2323

2424
import org.springframework.boot.context.TypeExcludeFilter;
25-
import org.springframework.boot.jackson.JacksonComponent;
2625
import org.springframework.boot.test.context.filter.annotation.StandardAnnotationCustomizableTypeExcludeFilter;
2726
import org.springframework.util.ClassUtils;
2827

@@ -35,21 +34,21 @@ class RestClientTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcl
3534

3635
private static final Class<?>[] NO_COMPONENTS = {};
3736

38-
private static final String DATABIND_MODULE_CLASS_NAME = "tools.jackson.databind.JacksonModule";
37+
private static final String[] OPTIONAL_INCLUDES = { "tools.jackson.databind.JacksonModule",
38+
"org.springframework.boot.jackson.JacksonComponent", "com.fasterxml.jackson.databind.Module",
39+
"org.springframework.boot.jackson2.JsonComponent" };
3940

4041
private static final Set<Class<?>> KNOWN_INCLUDES;
4142

4243
static {
4344
Set<Class<?>> includes = new LinkedHashSet<>();
44-
if (ClassUtils.isPresent(DATABIND_MODULE_CLASS_NAME, RestClientTypeExcludeFilter.class.getClassLoader())) {
45+
for (String optionalInclude : OPTIONAL_INCLUDES) {
4546
try {
46-
includes.add(Class.forName(DATABIND_MODULE_CLASS_NAME, true,
47-
RestClientTypeExcludeFilter.class.getClassLoader()));
47+
includes.add(ClassUtils.forName(optionalInclude, null));
4848
}
49-
catch (ClassNotFoundException ex) {
50-
throw new IllegalStateException("Failed to load " + DATABIND_MODULE_CLASS_NAME, ex);
49+
catch (Exception ex) {
50+
// Ignore
5151
}
52-
includes.add(JacksonComponent.class);
5352
}
5453
KNOWN_INCLUDES = Collections.unmodifiableSet(includes);
5554
}

module/spring-boot-webflux-test/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
optional(project(":core:spring-boot-autoconfigure"))
3232
optional(project(":module:spring-boot-http-codec"))
3333
optional(project(":module:spring-boot-jackson"))
34+
optional(project(":module:spring-boot-jackson2"))
3435
optional(project(":module:spring-boot-validation"))
3536
optional("org.junit.jupiter:junit-jupiter-api")
3637

module/spring-boot-webflux-test/src/main/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* <li>{@code @Controller}</li>
5151
* <li>{@code @ControllerAdvice}</li>
5252
* <li>{@code @JacksonComponent}</li>
53+
* <li>{@code @JsonComponent} (Jackson 2, deprecated)</li>
5354
* </ul>
5455
* <p>
5556
* as well as beans that implement:
@@ -58,6 +59,7 @@
5859
* <li>{@code GenericConverter}</li>
5960
* <li>{@code IDialect}, if Thymeleaf is available</li>
6061
* <li>{@code JacksonModule}, if Jackson is available</li>
62+
* <li>{@code Module}, if Jackson 2 is available (deprecated)</li>
6163
* <li>{@code WebExceptionHandler}</li>
6264
* <li>{@code WebFluxConfigurer}</li>
6365
* <li>{@code WebFilter}</li>

0 commit comments

Comments
 (0)