Browse Source

Align naming change for Jackson components

This commit polishes the renaming of JsonComponent to JacksonComponent
and the use of JacksonModule instead of Module in Jackson 2.

This also adds integration tests that use a JacksonComponent with
WebMvcTest and WebFluxTest.

Closes gh-47864
pull/47903/head
Stéphane Nicoll 2 months ago
parent
commit
30a6b50e82
  1. 8
      core/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java
  2. 4
      module/spring-boot-graphql-test/src/main/java/org/springframework/boot/graphql/test/autoconfigure/GraphQlTest.java
  3. 2
      module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/JacksonAutoConfigurationTests.java
  4. 2
      module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/jsontest/app/ExampleCustomObject.java
  5. 6
      module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/jsontest/app/ExampleJacksonComponent.java
  6. 8
      module/spring-boot-restclient-test/src/main/java/org/springframework/boot/restclient/test/autoconfigure/RestClientTest.java
  7. 6
      module/spring-boot-webclient-test/src/main/java/org/springframework/boot/webclient/test/autoconfigure/WebClientTest.java
  8. 4
      module/spring-boot-webflux-test/src/main/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTest.java
  9. 2
      module/spring-boot-webflux-test/src/main/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTypeExcludeFilter.java
  10. 8
      module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/ExampleController2.java
  11. 28
      module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/ExampleResult.java
  12. 38
      module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/ExampleResultSerializer.java
  13. 50
      module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTestJacksonComponentIntegrationTests.java
  14. 11
      module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTypeExcludeFilterTests.java
  15. 4
      module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/WebMvcTest.java
  16. 6
      module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/WebMvcTypeExcludeFilter.java
  17. 11
      module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/WebMvcTypeExcludeFilterTests.java
  18. 7
      module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/ExampleController2.java
  19. 28
      module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/ExampleResult.java
  20. 38
      module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/ExampleResultSerializer.java
  21. 47
      module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/WebMvcTestJacksonComponentIntegrationTests.java
  22. 4
      module/spring-boot-webservices-test/src/main/java/org/springframework/boot/webservices/test/autoconfigure/client/WebServiceClientTest.java

8
core/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java

@ -44,12 +44,12 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -44,12 +44,12 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* Using this annotation only enables auto-configuration that is relevant to JSON tests.
* Similarly, component scanning is limited to beans annotated with:
* <ul>
* <li>{@code @JsonComponent}</li>
* <li>{@code @JacksonComponent}</li>
* </ul>
* <p>
* as well as beans that implement:
* <ul>
* <li>{@code Module}, if Jackson is available</li>
* <li>{@code JacksonModule}, if Jackson is available</li>
* </ul>
* <p>
* By default, tests annotated with {@code JsonTest} will also initialize
@ -88,8 +88,8 @@ public @interface JsonTest { @@ -88,8 +88,8 @@ public @interface JsonTest {
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default only
* {@code @JsonComponent} and {@code Module} beans are included.
* {@link SpringBootApplication @SpringBootApplication}. By default, only
* {@code @JacksonComponent} and {@code JacksonModule} beans are included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used

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

@ -47,7 +47,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -47,7 +47,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* <ul>
* <li>{@code @Controller}
* <li>{@code RuntimeWiringConfigurer}
* <li>{@code @JsonComponent}
* <li>{@code @JacksonComponent}
* <li>{@code Converter}
* <li>{@code GenericConverter}
* <li>{@code DataFetcherExceptionResolver}
@ -121,7 +121,7 @@ public @interface GraphQlTest { @@ -121,7 +121,7 @@ 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},
* defined), {@code RuntimeWiringConfigurer}, {@code @JacksonComponent},
* {@code Converter}, {@code GenericConverter}, {@code DataFetcherExceptionResolver},
* {@code Instrumentation} and {@code GraphQlSourceBuilderCustomizer} beans are
* included.

2
module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/JacksonAutoConfigurationTests.java

@ -609,7 +609,7 @@ class JacksonAutoConfigurationTests { @@ -609,7 +609,7 @@ class JacksonAutoConfigurationTests {
}
@Test
void jsonComponentThatInjectsJsonMapperCausesBeanCurrentlyInCreationException() {
void jacksonComponentThatInjectsJsonMapperCausesBeanCurrentlyInCreationException() {
this.contextRunner.withUserConfiguration(CircularDependencySerializerConfiguration.class).run((context) -> {
assertThat(context).hasFailed();
assertThat(context).getFailure().hasRootCauseInstanceOf(BeanCurrentlyInCreationException.class);

2
module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/jsontest/app/ExampleCustomObject.java

@ -20,7 +20,7 @@ import java.util.Date; @@ -20,7 +20,7 @@ import java.util.Date;
import java.util.UUID;
/**
* Example object to read/write as JSON through {@link ExampleJsonComponent}.
* Example object to read/write as JSON through {@link ExampleJacksonComponent}.
*
* @author Phillip Webb
* @param value the value

6
module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/jsontest/app/ExampleJsonComponent.java → module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/jsontest/app/ExampleJacksonComponent.java

@ -31,13 +31,13 @@ import org.springframework.boot.jackson.ObjectValueSerializer; @@ -31,13 +31,13 @@ import org.springframework.boot.jackson.ObjectValueSerializer;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
/**
* Example {@link JacksonComponent @JsonComponent} for use with {@link JsonTest @JsonTest}
* tests.
* Example {@link JacksonComponent @JacksonComponent} for use with
* {@link JsonTest @JsonTest} tests.
*
* @author Phillip Webb
*/
@JacksonComponent
public class ExampleJsonComponent {
public class ExampleJacksonComponent {
static class Serializer extends ObjectValueSerializer<ExampleCustomObject> {

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

@ -45,12 +45,12 @@ import org.springframework.web.client.RestClient.Builder; @@ -45,12 +45,12 @@ import org.springframework.web.client.RestClient.Builder;
* Using this annotation only enables auto-configuration that is relevant to rest client
* tests. Similarly, component scanning is limited to beans annotated with:
* <ul>
* <li>{@code @JsonComponent}</li>
* <li>{@code @JacksonComponent}</li>
* </ul>
* <p>
* as well as beans that implement:
* <ul>
* <li>{@code Module}, if Jackson is available</li>
* <li>{@code JacksonModule}, if Jackson is available</li>
* </ul>
* <p>
* By default, tests annotated with {@code RestClientTest} will also auto-configure a
@ -107,8 +107,8 @@ public @interface RestClientTest { @@ -107,8 +107,8 @@ public @interface RestClientTest {
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default only
* {@code @JsonComponent} and {@code Module} beans are included.
* {@link SpringBootApplication @SpringBootApplication}. By default, only
* {@code @JacksonComponent} and {@code JacksonModule} beans are included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used

6
module/spring-boot-webclient-test/src/main/java/org/springframework/boot/webclient/test/autoconfigure/WebClientTest.java

@ -43,7 +43,7 @@ import org.springframework.web.client.RestClient.Builder; @@ -43,7 +43,7 @@ import org.springframework.web.client.RestClient.Builder;
* Using this annotation only enables auto-configuration that is relevant to rest client
* tests. Similarly, component scanning is limited to beans annotated with:
* <ul>
* <li>{@code @JsonComponent}</li>
* <li>{@code @JacksonComponent}</li>
* </ul>
* <p>
* as well as beans that implement:
@ -97,8 +97,8 @@ public @interface WebClientTest { @@ -97,8 +97,8 @@ public @interface WebClientTest {
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default only
* {@code @JsonComponent} and {@code Module} beans are included.
* {@link SpringBootApplication @SpringBootApplication}. By default, only
* {@code @JacksonComponent} and {@code JacksonModule} beans are included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used

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

@ -49,7 +49,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; @@ -49,7 +49,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
* <ul>
* <li>{@code @Controller}</li>
* <li>{@code @ControllerAdvice}</li>
* <li>{@code @JsonComponent}</li>
* <li>{@code @JacksonComponent}</li>
* </ul>
* <p>
* as well as beans that implement:
@ -57,7 +57,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; @@ -57,7 +57,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
* <li>{@code Converter}</li>
* <li>{@code GenericConverter}</li>
* <li>{@code IDialect}, if Thymeleaf is available</li>
* <li>{@code Module}, if Jackson is available</li>
* <li>{@code JacksonModule}, if Jackson is available</li>
* <li>{@code WebExceptionHandler}</li>
* <li>{@code WebFluxConfigurer}</li>
* <li>{@code WebFilter}</li>

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

@ -43,7 +43,7 @@ class WebFluxTypeExcludeFilter extends StandardAnnotationCustomizableTypeExclude @@ -43,7 +43,7 @@ class WebFluxTypeExcludeFilter extends StandardAnnotationCustomizableTypeExclude
private static final Class<?>[] NO_CONTROLLERS = {};
private static final String[] OPTIONAL_INCLUDES = { "tools.jackson.databind.JacksonModule",
"org.springframework.boot.jackson.JsonComponent" };
"org.springframework.boot.jackson.JacksonComponent" };
private static final Set<Class<?>> KNOWN_INCLUDES;

8
module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/ExampleController2.java

@ -21,6 +21,8 @@ import reactor.core.publisher.Mono; @@ -21,6 +21,8 @@ import reactor.core.publisher.Mono;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
@ -42,4 +44,10 @@ public class ExampleController2 { @@ -42,4 +44,10 @@ public class ExampleController2 {
return Mono.just(id.getId() + "two");
}
@PostMapping("/two/{id}")
@ResponseBody
public Mono<ExampleResult> twoUpdate(@PathVariable String id) {
return Mono.just(new ExampleResult(id));
}
}

28
module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/ExampleResult.java

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.webflux.test.autoconfigure;
import org.springframework.boot.jackson.JacksonComponent;
/**
* Example result that requires a {@link JacksonComponent}.
*
* @param id sample data
* @author Stephane Nicoll
*/
public record ExampleResult(String id) {
}

38
module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/ExampleResultSerializer.java

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.webflux.test.autoconfigure;
import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializationContext;
import org.springframework.boot.jackson.JacksonComponent;
import org.springframework.boot.jackson.ObjectValueSerializer;
/**
* {@link ObjectValueSerializer} for {@link ExampleResult}.
*
* @author Stephane Nicoll
*/
@JacksonComponent
public class ExampleResultSerializer extends ObjectValueSerializer<ExampleResult> {
@Override
protected void serializeObject(ExampleResult item, JsonGenerator jsonWriter, SerializationContext context) {
jsonWriter.writeStringProperty("identifier", item.id());
}
}

50
module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTestJacksonComponentIntegrationTests.java

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.webflux.test.autoconfigure;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JacksonComponent;
import org.springframework.test.json.JsonCompareMode;
import org.springframework.test.web.reactive.server.WebTestClient;
/**
* Tests for {@link WebFluxTest @WebFluxTest} to validate {@link JacksonComponent} beans
* are discovered.
*
* @author Stephane Nicoll
*/
@WebFluxTest(controllers = ExampleController2.class)
class WebFluxTestJacksonComponentIntegrationTests {
@Autowired
private WebTestClient webClient;
@Test
void shouldFindConverter() {
this.webClient.post()
.uri("/two/" + "1234abcd")
.exchange()
.expectStatus()
.isOk()
.expectBody()
.json("{ \"identifier\": \"1234abcd\" }", JsonCompareMode.LENIENT);
}
}

11
module/spring-boot-webflux-test/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebFluxTypeExcludeFilterTests.java

@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import tools.jackson.databind.module.SimpleModule;
import org.springframework.boot.jackson.JacksonComponent;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.type.classreading.MetadataReader;
@ -59,6 +60,7 @@ class WebFluxTypeExcludeFilterTests { @@ -59,6 +60,7 @@ class WebFluxTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebFilter.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
@Test
@ -72,6 +74,7 @@ class WebFluxTypeExcludeFilterTests { @@ -72,6 +74,7 @@ class WebFluxTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebFilter.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
@Test
@ -85,6 +88,7 @@ class WebFluxTypeExcludeFilterTests { @@ -85,6 +88,7 @@ class WebFluxTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebFilter.class)).isTrue();
assertThat(excludes(filter, ExampleModule.class)).isTrue();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isTrue();
}
@Test
@ -98,6 +102,7 @@ class WebFluxTypeExcludeFilterTests { @@ -98,6 +102,7 @@ class WebFluxTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isFalse();
assertThat(excludes(filter, ExampleWebFilter.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
@Test
@ -111,6 +116,7 @@ class WebFluxTypeExcludeFilterTests { @@ -111,6 +116,7 @@ class WebFluxTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebFilter.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
private boolean excludes(WebFluxTypeExcludeFilter filter, Class<?> type) throws IOException {
@ -185,4 +191,9 @@ class WebFluxTypeExcludeFilterTests { @@ -185,4 +191,9 @@ class WebFluxTypeExcludeFilterTests {
}
@JacksonComponent
static class ExampleJacksonComponent {
}
}

4
module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/WebMvcTest.java

@ -47,7 +47,7 @@ import org.springframework.test.web.servlet.MockMvc; @@ -47,7 +47,7 @@ import org.springframework.test.web.servlet.MockMvc;
* <ul>
* <li>{@code @Controller}</li>
* <li>{@code @ControllerAdvice}</li>
* <li>{@code @JsonComponent}</li>
* <li>{@code @JacksonComponent}</li>
* </ul>
* <p>
* as well as beans that implement:
@ -62,7 +62,7 @@ import org.springframework.test.web.servlet.MockMvc; @@ -62,7 +62,7 @@ import org.springframework.test.web.servlet.MockMvc;
* <li>{@code HandlerMethodArgumentResolver}</li>
* <li>{@code HttpMessageConverter}</li>
* <li>{@code IDialect}, if Thymeleaf is available</li>
* <li>{@code Module}, if Jackson is available</li>
* <li>{@code JacksonModule}, if Jackson is available</li>
* <li>{@code SecurityFilterChain}</li>
* <li>{@code WebMvcConfigurer}</li>
* <li>{@code WebMvcRegistrations}</li>

6
module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/WebMvcTypeExcludeFilter.java

@ -49,10 +49,10 @@ class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeF @@ -49,10 +49,10 @@ class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeF
private static final Class<?>[] NO_CONTROLLERS = {};
private static final String[] OPTIONAL_INCLUDES = { "org.springframework.boot.jackson.JsonComponent",
private static final String[] OPTIONAL_INCLUDES = { "tools.jackson.databind.JacksonModule",
"org.springframework.boot.jackson.JacksonComponent",
"org.springframework.security.config.annotation.web.WebSecurityConfigurer",
"org.springframework.security.web.SecurityFilterChain", "org.thymeleaf.dialect.IDialect",
"tools.jackson.databind.JacksonModule" };
"org.springframework.security.web.SecurityFilterChain", "org.thymeleaf.dialect.IDialect", };
private static final Set<Class<?>> KNOWN_INCLUDES;

11
module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/WebMvcTypeExcludeFilterTests.java

@ -21,6 +21,7 @@ import java.io.IOException; @@ -21,6 +21,7 @@ import java.io.IOException;
import org.junit.jupiter.api.Test;
import tools.jackson.databind.module.SimpleModule;
import org.springframework.boot.jackson.JacksonComponent;
import org.springframework.boot.webmvc.autoconfigure.WebMvcRegistrations;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
@ -60,6 +61,7 @@ class WebMvcTypeExcludeFilterTests { @@ -60,6 +61,7 @@ class WebMvcTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
@Test
@ -75,6 +77,7 @@ class WebMvcTypeExcludeFilterTests { @@ -75,6 +77,7 @@ class WebMvcTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
@Test
@ -90,6 +93,7 @@ class WebMvcTypeExcludeFilterTests { @@ -90,6 +93,7 @@ class WebMvcTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isTrue();
assertThat(excludes(filter, ExampleModule.class)).isTrue();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isTrue();
}
@Test
@ -105,6 +109,7 @@ class WebMvcTypeExcludeFilterTests { @@ -105,6 +109,7 @@ class WebMvcTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isFalse();
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
@Test
@ -120,6 +125,7 @@ class WebMvcTypeExcludeFilterTests { @@ -120,6 +125,7 @@ class WebMvcTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleHandlerInterceptor.class)).isFalse();
assertThat(excludes(filter, ExampleModule.class)).isFalse();
assertThat(excludes(filter, ExampleJacksonComponent.class)).isFalse();
}
private boolean excludes(WebMvcTypeExcludeFilter filter, Class<?> type) throws IOException {
@ -198,4 +204,9 @@ class WebMvcTypeExcludeFilterTests { @@ -198,4 +204,9 @@ class WebMvcTypeExcludeFilterTests {
}
@JacksonComponent
static class ExampleJacksonComponent {
}
}

7
module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/ExampleController2.java

@ -20,6 +20,7 @@ import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; @@ -20,6 +20,7 @@ import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
@ -42,4 +43,10 @@ public class ExampleController2 { @@ -42,4 +43,10 @@ public class ExampleController2 {
return id.getId() + "two";
}
@PostMapping("/two/{id}")
@ResponseBody
public ExampleResult twoUpdate(@PathVariable String id) {
return new ExampleResult(id);
}
}

28
module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/ExampleResult.java

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.webmvc.test.autoconfigure.mockmvc;
import org.springframework.boot.jackson.JacksonComponent;
/**
* Example result that requires a {@link JacksonComponent}.
*
* @param id sample data
* @author Stephane Nicoll
*/
public record ExampleResult(String id) {
}

38
module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/ExampleResultSerializer.java

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.webmvc.test.autoconfigure.mockmvc;
import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializationContext;
import org.springframework.boot.jackson.JacksonComponent;
import org.springframework.boot.jackson.ObjectValueSerializer;
/**
* {@link ObjectValueSerializer} for {@link ExampleResult}.
*
* @author Stephane Nicoll
*/
@JacksonComponent
public class ExampleResultSerializer extends ObjectValueSerializer<ExampleResult> {
@Override
protected void serializeObject(ExampleResult item, JsonGenerator jsonWriter, SerializationContext context) {
jsonWriter.writeStringProperty("identifier", item.id());
}
}

47
module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/WebMvcTestJacksonComponentIntegrationTests.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.webmvc.test.autoconfigure.mockmvc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JacksonComponent;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WebMvcTest @WebMvcTest} to validate {@link JacksonComponent} beans are
* discovered.
*
* @author Stephane Nicoll
*/
@WebMvcTest(controllers = ExampleController2.class)
class WebMvcTestJacksonComponentIntegrationTests {
@Autowired
private MockMvcTester mvc;
@Test
void shouldFindJacksonComponent() {
assertThat(this.mvc.post().uri("/two/1234abcd")).hasStatusOk().bodyJson().isLenientlyEqualTo("""
{ "identifier": "1234abcd" }
""");
}
}

4
module/spring-boot-webservices-test/src/main/java/org/springframework/boot/webservices/test/autoconfigure/client/WebServiceClientTest.java

@ -95,8 +95,8 @@ public @interface WebServiceClientTest { @@ -95,8 +95,8 @@ public @interface WebServiceClientTest {
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default only
* {@code @JsonComponent} and {@code Module} beans are included.
* {@link SpringBootApplication @SpringBootApplication}. By default, only
* {@code @JacksonComponent} and {@code JacksonModule} beans are included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used

Loading…
Cancel
Save