Skip to content

Commit 19e2fca

Browse files
committed
Add JacksonExceptions Test
1 parent 277ba78 commit 19e2fca

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.jackson.exception;
2+
3+
public class User {
4+
public int id;
5+
public String name;
6+
7+
public User(final int id, final String name) {
8+
this.id = id;
9+
this.name = name;
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.jackson.exception;
2+
3+
public class UserWithConflict {
4+
public int id;
5+
public String name;
6+
boolean checked;
7+
8+
public UserWithConflict() {
9+
super();
10+
}
11+
12+
public UserWithConflict(final int id, final String name, final boolean checked) {
13+
this.id = id;
14+
this.name = name;
15+
this.checked = checked;
16+
}
17+
18+
public boolean getChecked() {
19+
return checked;
20+
}
21+
22+
public boolean isChecked() {
23+
return checked;
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.jackson.exception;
2+
3+
public class UserWithPrivateFields {
4+
int id;
5+
String name;
6+
7+
public UserWithPrivateFields() {
8+
super();
9+
}
10+
11+
public UserWithPrivateFields(final int id, final String name) {
12+
this.id = id;
13+
this.name = name;
14+
}
15+
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.jackson.exception;
2+
3+
import com.fasterxml.jackson.annotation.JsonRootName;
4+
5+
@JsonRootName(value = "user")
6+
public class UserWithRoot {
7+
public int id;
8+
public String name;
9+
10+
public UserWithRoot() {
11+
super();
12+
}
13+
14+
public UserWithRoot(final int id, final String name) {
15+
this.id = id;
16+
this.name = name;
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.jackson.exception;
2+
3+
public class Zoo {
4+
public Animal animal;
5+
}
6+
7+
abstract class Animal {
8+
public String name;
9+
10+
protected Animal() {
11+
}
12+
}
13+
14+
class Cat extends Animal {
15+
public int lives;
16+
17+
public Cat() {
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.jackson.exception;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
5+
public class ZooConfigured {
6+
public AnimalConfigured animal;
7+
}
8+
9+
@JsonDeserialize(as = CatConfigured.class)
10+
abstract class AnimalConfigured {
11+
public String name;
12+
13+
protected AnimalConfigured() {
14+
}
15+
}
16+
17+
class CatConfigured extends AnimalConfigured {
18+
public int lives;
19+
20+
public CatConfigured() {
21+
}
22+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package org.baeldung.jackson.test;
2+
3+
import static org.hamcrest.Matchers.containsString;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
import org.baeldung.jackson.dtos.User;
11+
import org.baeldung.jackson.exception.UserWithPrivateFields;
12+
import org.baeldung.jackson.exception.UserWithRoot;
13+
import org.baeldung.jackson.exception.Zoo;
14+
import org.baeldung.jackson.exception.ZooConfigured;
15+
import org.junit.Test;
16+
17+
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
18+
import com.fasterxml.jackson.annotation.PropertyAccessor;
19+
import com.fasterxml.jackson.core.JsonFactory;
20+
import com.fasterxml.jackson.core.JsonParseException;
21+
import com.fasterxml.jackson.core.JsonParser;
22+
import com.fasterxml.jackson.core.JsonProcessingException;
23+
import com.fasterxml.jackson.core.type.TypeReference;
24+
import com.fasterxml.jackson.databind.DeserializationFeature;
25+
import com.fasterxml.jackson.databind.JsonMappingException;
26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
28+
29+
public class JacksonExceptionsTest {
30+
31+
// JsonMappingException: Can not construct instance of
32+
@Test(expected = JsonMappingException.class)
33+
public void givenAbstractClass_whenDeserializing_thenException() throws IOException {
34+
final String json = "{\"animal\":{\"name\":\"lacy\"}}";
35+
final ObjectMapper mapper = new ObjectMapper();
36+
37+
mapper.reader().withType(Zoo.class).readValue(json);
38+
}
39+
40+
@Test
41+
public void givenAbstractClassConfigured_whenDeserializing_thenCorrect() throws IOException {
42+
final String json = "{\"animal\":{\"name\":\"lacy\"}}";
43+
final ObjectMapper mapper = new ObjectMapper();
44+
45+
mapper.reader().withType(ZooConfigured.class).readValue(json);
46+
}
47+
48+
// JsonMappingException: No serializer found for class
49+
@Test(expected = JsonMappingException.class)
50+
public void givenClassWithPrivateFields_whenSerializing_thenException() throws IOException {
51+
final UserWithPrivateFields user = new UserWithPrivateFields(1, "John");
52+
53+
final ObjectMapper mapper = new ObjectMapper();
54+
mapper.writer().writeValueAsString(user);
55+
}
56+
57+
@Test
58+
public void givenClassWithPrivateFields_whenConfigureSerializing_thenCorrect() throws IOException {
59+
final UserWithPrivateFields user = new UserWithPrivateFields(1, "John");
60+
61+
final ObjectMapper mapper = new ObjectMapper();
62+
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
63+
64+
final String result = mapper.writer().writeValueAsString(user);
65+
assertThat(result, containsString("John"));
66+
}
67+
68+
// JsonMappingException: No suitable constructor found
69+
@Test(expected = JsonMappingException.class)
70+
public void givenNoDefaultConstructor_whenDeserializing_thenException() throws IOException {
71+
final String json = "{\"id\":1,\"name\":\"John\"}";
72+
final ObjectMapper mapper = new ObjectMapper();
73+
74+
mapper.reader().withType(org.baeldung.jackson.exception.User.class).readValue(json);
75+
}
76+
77+
@Test
78+
public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException {
79+
final String json = "{\"id\":1,\"name\":\"John\"}";
80+
final ObjectMapper mapper = new ObjectMapper();
81+
82+
final User user = mapper.reader().withType(User.class).readValue(json);
83+
assertEquals("John", user.name);
84+
}
85+
86+
// JsonMappingException: Root name does not match expected
87+
@Test(expected = JsonMappingException.class)
88+
public void givenWrappedJsonString_whenDeserializing_thenException() throws IOException {
89+
final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}";
90+
91+
final ObjectMapper mapper = new ObjectMapper();
92+
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
93+
94+
mapper.reader().withType(User.class).readValue(json);
95+
}
96+
97+
@Test
98+
public void givenWrappedJsonStringAndConfigureClass_whenDeserializing_thenCorrect() throws IOException {
99+
final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}";
100+
101+
final ObjectMapper mapper = new ObjectMapper();
102+
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
103+
104+
final UserWithRoot user = mapper.reader().withType(UserWithRoot.class).readValue(json);
105+
assertEquals("John", user.name);
106+
}
107+
108+
// JsonMappingException: Can not deserialize instance of
109+
@Test(expected = JsonMappingException.class)
110+
public void givenJsonOfArray_whenDeserializing_thenException() throws JsonProcessingException, IOException {
111+
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
112+
final ObjectMapper mapper = new ObjectMapper();
113+
114+
mapper.reader().withType(User.class).readValue(json);
115+
}
116+
117+
@Test
118+
public void givenJsonOfArray_whenDeserializing_thenCorrect() throws JsonProcessingException, IOException {
119+
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
120+
final ObjectMapper mapper = new ObjectMapper();
121+
122+
final List<User> users = mapper.reader().withType(new TypeReference<List<User>>() {
123+
}).readValue(json);
124+
125+
assertEquals(2, users.size());
126+
}
127+
128+
// UnrecognizedPropertyException
129+
@Test(expected = UnrecognizedPropertyException.class)
130+
public void givenJsonStringWithExtra_whenDeserializing_thenException() throws IOException {
131+
final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}";
132+
133+
final ObjectMapper mapper = new ObjectMapper();
134+
mapper.reader().withType(User.class).readValue(json);
135+
}
136+
137+
@Test
138+
public void givenJsonStringWithExtra_whenConfigureDeserializing_thenCorrect() throws IOException {
139+
final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}";
140+
141+
final ObjectMapper mapper = new ObjectMapper();
142+
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
143+
144+
final User user = mapper.reader().withType(User.class).readValue(json);
145+
assertEquals("John", user.name);
146+
}
147+
148+
// JsonParseException: Unexpected character (''' (code 39))
149+
@Test(expected = JsonParseException.class)
150+
public void givenStringWithSingleQuotes_whenDeserializing_thenException() throws JsonProcessingException, IOException {
151+
final String json = "{'id':1,'name':'John'}";
152+
final ObjectMapper mapper = new ObjectMapper();
153+
154+
mapper.reader().withType(User.class).readValue(json);
155+
}
156+
157+
@Test
158+
public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect() throws JsonProcessingException, IOException {
159+
final String json = "{'id':1,'name':'John'}";
160+
161+
final JsonFactory factory = new JsonFactory();
162+
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
163+
final ObjectMapper mapper = new ObjectMapper(factory);
164+
165+
final User user = mapper.reader().withType(User.class).readValue(json);
166+
assertEquals("John", user.name);
167+
}
168+
169+
170+
171+
}

0 commit comments

Comments
 (0)