Skip to content

Commit 9d954b0

Browse files
author
eugenp
committed
minor jackson testing and cleanup work
1 parent 502e405 commit 9d954b0

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

‎httpclient/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<dependency>
1515
<groupId>com.google.guava</groupId>
1616
<artifactId>guava</artifactId>
17-
<version>15.0</version>
17+
<version>${guava.version}</version>
1818
</dependency>
1919

2020
<dependency>

‎jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111
import org.junit.Test;
1212

1313
import com.fasterxml.jackson.core.JsonParseException;
14-
import com.fasterxml.jackson.core.JsonParser;
15-
import com.fasterxml.jackson.core.JsonProcessingException;
16-
import com.fasterxml.jackson.databind.DeserializationContext;
1714
import com.fasterxml.jackson.databind.DeserializationFeature;
18-
import com.fasterxml.jackson.databind.JsonDeserializer;
1915
import com.fasterxml.jackson.databind.JsonMappingException;
2016
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
2217
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
2318

2419
public class JacksonDeserializationUnitTest {
@@ -45,6 +40,7 @@ public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToACla
4540

4641
assertNotNull(readValue);
4742
assertThat(readValue.getStringValue(), equalTo("a"));
43+
assertThat(readValue.isBooleanValue(), equalTo(true));
4844
}
4945

5046
@Test(expected = UnrecognizedPropertyException.class)
@@ -61,8 +57,12 @@ public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenEx
6157
}
6258

6359
@Test
64-
public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
65-
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
60+
public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
61+
final String jsonAsString =// @formatter:off
62+
"{\"stringValue\":\"a\"," +
63+
"\"intValue\":1," +
64+
"\"booleanValue\":true," +
65+
"\"stringValue2\":\"something\"}"; // @formatter:on
6666
final ObjectMapper mapper = new ObjectMapper();
6767
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
6868

@@ -75,24 +75,20 @@ public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenD
7575
}
7676

7777
@Test
78-
public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
79-
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
78+
public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
79+
final String jsonAsString =// @formatter:off
80+
"{\"stringValue\":\"a\"," +
81+
"\"intValue\":1," +
82+
"\"booleanValue\":true," +
83+
"\"stringValue2\":\"something\"}"; // @formatter:on
8084
final ObjectMapper mapper = new ObjectMapper();
81-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
8285

8386
final MyDtoIgnoreUnkown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnkown.class);
8487

8588
assertNotNull(readValue);
8689
assertThat(readValue.getStringValue(), equalTo("a"));
8790
assertThat(readValue.isBooleanValue(), equalTo(true));
8891
assertThat(readValue.getIntValue(), equalTo(1));
89-
90-
mapper.addHandler(new DeserializationProblemHandler() {
91-
@Override
92-
public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException, JsonProcessingException {
93-
return super.handleUnknownProperty(ctxt, jp, deserializer, beanOrClass, propertyName);
94-
}
95-
});
9692
}
9793

9894
}

0 commit comments

Comments
 (0)