Skip to content

Commit b61e047

Browse files
committed
Unit test changes for improved code coverage
1 parent cfef132 commit b61e047

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fasterxml.jackson.databind.module;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.IOException;
5+
import java.io.StringReader;
46
import java.lang.reflect.Type;
57
import java.util.*;
68

@@ -239,29 +241,68 @@ public void serialize(Test3787Bean value, JsonGenerator gen, SerializerProvider
239241
/**********************************************************
240242
*/
241243

244+
private final ObjectMapper MAPPER = newJsonMapper();
245+
246+
@Test
247+
public void testDeserializationWithoutModule() throws Exception
248+
{
249+
final String DOC = "{\"str\":\"ab\",\"num\":2}";
250+
251+
try {
252+
MAPPER.readValue(DOC, CustomBean.class);
253+
fail("Should have caused an exception");
254+
} catch (DatabindException e) {
255+
verifyException(e, "Cannot construct");
256+
verifyException(e, "no creators");
257+
}
258+
259+
// And then other variations
260+
try {
261+
MAPPER.readValue(new StringReader(DOC), CustomBean.class);
262+
fail("Should have caused an exception");
263+
} catch (DatabindException e) {
264+
verifyException(e, "Cannot construct");
265+
verifyException(e, "no creators");
266+
}
267+
268+
try {
269+
MAPPER.readValue(utf8Bytes(DOC), CustomBean.class);
270+
fail("Should have caused an exception");
271+
} catch (DatabindException e) {
272+
verifyException(e, "Cannot construct");
273+
verifyException(e, "no creators");
274+
}
275+
276+
try {
277+
MAPPER.readValue(new ByteArrayInputStream(utf8Bytes(DOC)), CustomBean.class);
278+
fail("Should have caused an exception");
279+
} catch (DatabindException e) {
280+
verifyException(e, "Cannot construct");
281+
verifyException(e, "no creators");
282+
}
283+
}
284+
242285
/**
243286
* Basic test to ensure we do not have functioning default
244287
* serializers for custom types used in tests.
245288
*/
246289
@Test
247-
public void testWithoutModule()
290+
public void testSerializationWithoutModule() throws Exception
248291
{
249-
ObjectMapper mapper = new ObjectMapper();
250292
// first: serialization failure:
251293
try {
252-
mapper.writeValueAsString(new CustomBean("foo", 3));
294+
MAPPER.writeValueAsString(new CustomBean("foo", 3));
253295
fail("Should have caused an exception");
254-
} catch (IOException e) {
296+
} catch (DatabindException e) {
255297
verifyException(e, "No serializer found");
256298
}
257299

258-
// then deserialization
300+
// and with another write call for test coverage
259301
try {
260-
mapper.readValue("{\"str\":\"ab\",\"num\":2}", CustomBean.class);
302+
MAPPER.writeValueAsBytes(new CustomBean("foo", 3));
261303
fail("Should have caused an exception");
262-
} catch (IOException e) {
263-
verifyException(e, "Cannot construct");
264-
verifyException(e, "no creators");
304+
} catch (DatabindException e) {
305+
verifyException(e, "No serializer found");
265306
}
266307
}
267308

src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.fasterxml.jackson.annotation.JsonCreator;
1111
import com.fasterxml.jackson.annotation.JsonInclude;
1212
import com.fasterxml.jackson.annotation.JsonValue;
13-
13+
import com.fasterxml.jackson.core.JsonParser;
1414
import com.fasterxml.jackson.databind.*;
1515
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1616
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
@@ -441,7 +441,7 @@ public void testFailOnDupKeys() throws Exception
441441

442442
// first: verify defaults:
443443
assertFalse(MAPPER.isEnabled(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY));
444-
ObjectNode root = (ObjectNode) MAPPER.readTree(DUP_JSON);
444+
ObjectNode root = _objNode(DUP_JSON);
445445
assertEquals(2, root.path("a").asInt());
446446

447447
// and then enable checks:
@@ -609,7 +609,10 @@ public void testRemoveNulls() throws Exception
609609
}
610610

611611
private ObjectNode _objNode(String json) throws Exception {
612-
return (ObjectNode) MAPPER.readTree(a2q(json));
612+
// Use different read method for better code coverage
613+
try (JsonParser p = MAPPER.createParser(a2q(json))) {
614+
return (ObjectNode) MAPPER.reader().readTree(p);
615+
}
613616
}
614617

615618
private String _toString(JsonNode n) {

0 commit comments

Comments
 (0)