Skip to content

Commit 274ef6f

Browse files
committed
fixup! Add Jackson2Tester support
1 parent 2db42f9 commit 274ef6f

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

core/spring-boot-test/src/main/java/org/springframework/boot/test/json/Jackson2Tester.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import com.fasterxml.jackson.databind.ObjectReader;
2626
import com.fasterxml.jackson.databind.ObjectWriter;
27-
import com.fasterxml.jackson.databind.json.JsonMapper;
2827
import com.jayway.jsonpath.Configuration;
2928
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
3029
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
@@ -35,16 +34,16 @@
3534
import org.springframework.util.Assert;
3635

3736
/**
38-
* AssertJ based JSON tester backed by Jackson. Usually instantiated via
39-
* {@link #initFields(Object, JsonMapper)}, for example: <pre class="code">
37+
* AssertJ based JSON tester backed by Jackson 2. Usually instantiated via
38+
* {@link #initFields(Object, ObjectMapper)}, for example: <pre class="code">
4039
* public class ExampleObjectJsonTests {
4140
*
42-
* private JacksonTester&lt;ExampleObject&gt; json;
41+
* private Jackson2Tester&lt;ExampleObject&gt; json;
4342
*
4443
* &#064;Before
4544
* public void setup() {
46-
* JsonMapper jsonMapper = new JsonMapper();
47-
* JacksonTester.initFields(this, jsonMapper);
45+
* ObjectMapper objectMapper = new ObjectMapper();
46+
* JacksonTester.initFields(this, objectMapper);
4847
* }
4948
*
5049
* &#064;Test
@@ -156,24 +155,22 @@ private JavaType getType(ResolvableType type) {
156155
* Utility method to initialize {@link Jackson2Tester} fields. See
157156
* {@link Jackson2Tester class-level documentation} for example usage.
158157
* @param testInstance the test instance
159-
* @param jsonMapper the JSON mapper
160-
* @since 4.0.0
161-
* @see #initFields(Object, JsonMapper)
158+
* @param objectMapper the JSON mapper
159+
* @see #initFields(Object, ObjectMapper)
162160
*/
163-
public static void initFields(Object testInstance, JsonMapper jsonMapper) {
164-
new Jackson2FieldInitializer().initFields(testInstance, jsonMapper);
161+
public static void initFields(Object testInstance, ObjectMapper objectMapper) {
162+
new Jackson2FieldInitializer().initFields(testInstance, objectMapper);
165163
}
166164

167165
/**
168166
* Utility method to initialize {@link Jackson2Tester} fields. See
169167
* {@link Jackson2Tester class-level documentation} for example usage.
170168
* @param testInstance the test instance
171-
* @param jsonMapperFactory a factory to create the JSON mapper
172-
* @since 4.0.0
173-
* @see #initFields(Object, JsonMapper)
169+
* @param objectMapperFactory a factory to create the object mapper
170+
* @see #initFields(Object, ObjectMapper)
174171
*/
175-
public static void initFields(Object testInstance, ObjectFactory<JsonMapper> jsonMapperFactory) {
176-
new Jackson2FieldInitializer().initFields(testInstance, jsonMapperFactory);
172+
public static void initFields(Object testInstance, ObjectFactory<ObjectMapper> objectMapperFactory) {
173+
new Jackson2FieldInitializer().initFields(testInstance, objectMapperFactory);
177174
}
178175

179176
/**
@@ -193,15 +190,15 @@ public Jackson2Tester<T> forView(Class<?> view) {
193190
/**
194191
* {@link FieldInitializer} for Jackson.
195192
*/
196-
private static class Jackson2FieldInitializer extends FieldInitializer<JsonMapper> {
193+
private static class Jackson2FieldInitializer extends FieldInitializer<ObjectMapper> {
197194

198195
protected Jackson2FieldInitializer() {
199196
super(Jackson2Tester.class);
200197
}
201198

202199
@Override
203200
protected AbstractJsonMarshalTester<Object> createTester(Class<?> resourceLoadClass, ResolvableType type,
204-
JsonMapper marshaller) {
201+
ObjectMapper marshaller) {
205202
return new Jackson2Tester<>(resourceLoadClass, type, marshaller);
206203
}
207204

core/spring-boot-test/src/test/java/org/springframework/boot/test/json/Jackson2TesterTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.util.List;
2020

21-
import com.fasterxml.jackson.databind.json.JsonMapper;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
2222
import org.jspecify.annotations.Nullable;
2323
import org.junit.jupiter.api.Test;
2424

@@ -40,15 +40,15 @@ class Jackson2TesterTests extends AbstractJsonMarshalTesterTests {
4040
@Test
4141
@SuppressWarnings("NullAway") // Test null check
4242
void initFieldsWhenTestIsNullShouldThrowException() {
43-
assertThatIllegalArgumentException().isThrownBy(() -> Jackson2Tester.initFields(null, new JsonMapper()))
43+
assertThatIllegalArgumentException().isThrownBy(() -> Jackson2Tester.initFields(null, new ObjectMapper()))
4444
.withMessageContaining("'testInstance' must not be null");
4545
}
4646

4747
@Test
4848
@SuppressWarnings("NullAway") // Test null check
4949
void initFieldsWhenMarshallerIsNullShouldThrowException() {
5050
assertThatIllegalArgumentException()
51-
.isThrownBy(() -> Jackson2Tester.initFields(new InitFieldsTestClass(), (JsonMapper) null))
51+
.isThrownBy(() -> Jackson2Tester.initFields(new InitFieldsTestClass(), (ObjectMapper) null))
5252
.withMessageContaining("'marshaller' must not be null");
5353
}
5454

@@ -57,7 +57,7 @@ void initFieldsShouldSetNullFields() {
5757
InitFieldsTestClass test = new InitFieldsTestClass();
5858
assertThat(test.test).isNull();
5959
assertThat(test.base).isNull();
60-
Jackson2Tester.initFields(test, new JsonMapper());
60+
Jackson2Tester.initFields(test, new ObjectMapper());
6161
assertThat(test.test).isNotNull();
6262
assertThat(test.base).isNotNull();
6363
ResolvableType type = test.test.getType();
@@ -68,15 +68,15 @@ void initFieldsShouldSetNullFields() {
6868

6969
@Override
7070
protected AbstractJsonMarshalTester<Object> createTester(Class<?> resourceLoadClass, ResolvableType type) {
71-
return new org.springframework.boot.test.json.Jackson2Tester<>(resourceLoadClass, type, new JsonMapper());
71+
return new org.springframework.boot.test.json.Jackson2Tester<>(resourceLoadClass, type, new ObjectMapper());
7272
}
7373

7474
abstract static class InitFieldsBaseClass {
7575

7676
public org.springframework.boot.test.json.@Nullable Jackson2Tester<ExampleObject> base;
7777

7878
public org.springframework.boot.test.json.Jackson2Tester<ExampleObject> baseSet = new org.springframework.boot.test.json.Jackson2Tester<>(
79-
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class), new JsonMapper());
79+
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class), new ObjectMapper());
8080

8181
}
8282

@@ -85,7 +85,7 @@ static class InitFieldsTestClass extends InitFieldsBaseClass {
8585
public org.springframework.boot.test.json.@Nullable Jackson2Tester<List<ExampleObject>> test;
8686

8787
public org.springframework.boot.test.json.Jackson2Tester<ExampleObject> testSet = new org.springframework.boot.test.json.Jackson2Tester<>(
88-
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class), new JsonMapper());
88+
InitFieldsBaseClass.class, ResolvableType.forClass(ExampleObject.class), new ObjectMapper());
8989

9090
}
9191

0 commit comments

Comments
 (0)