Skip to content

Commit 366098f

Browse files
author
Eugen
committed
Merge pull request eugenp#109 from Doha2012/master
Modify jackson bidirectional relationship test
2 parents f475856 + eb28e07 commit 366098f

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,90 +26,82 @@
2626
public class JacksonBidirectionRelationTest {
2727

2828
@Test(expected = JsonMappingException.class)
29-
public void givenBidirectionRelation_whenSerialize_thenException() throws JsonProcessingException {
29+
public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException {
3030
final User user = new User(1, "John");
3131
final Item item = new Item(2, "book", user);
3232
user.addItem(item);
3333

34-
final ObjectMapper mapper = new ObjectMapper();
35-
mapper.writeValueAsString(item);
34+
new ObjectMapper().writeValueAsString(item);
3635
}
3736

3837
@Test
39-
public void givenBidirectionRelation_whenUseJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
38+
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
4039
final UserWithRef user = new UserWithRef(1, "John");
4140
final ItemWithRef item = new ItemWithRef(2, "book", user);
4241
user.addItem(item);
4342

44-
final ObjectMapper mapper = new ObjectMapper();
45-
final String result = mapper.writeValueAsString(item);
43+
final String result = new ObjectMapper().writeValueAsString(item);
4644

4745
assertThat(result, containsString("book"));
4846
assertThat(result, containsString("John"));
4947
assertThat(result, not(containsString("userItems")));
5048
}
5149

5250
@Test
53-
public void givenBidirectionRelation_whenUseJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
51+
public void givenBidirectionRelation_whenUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
5452
final UserWithIdentity user = new UserWithIdentity(1, "John");
5553
final ItemWithIdentity item = new ItemWithIdentity(2, "book", user);
5654
user.addItem(item);
5755

58-
final ObjectMapper mapper = new ObjectMapper();
59-
final String result = mapper.writeValueAsString(item);
56+
final String result = new ObjectMapper().writeValueAsString(item);
6057

6158
assertThat(result, containsString("book"));
6259
assertThat(result, containsString("John"));
6360
assertThat(result, containsString("userItems"));
6461
}
6562

6663
@Test
67-
public void givenBidirectionRelation_whenUseJsonIgnore_thenCorrect() throws JsonProcessingException {
64+
public void givenBidirectionRelation_whenUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
6865
final UserWithIgnore user = new UserWithIgnore(1, "John");
6966
final ItemWithIgnore item = new ItemWithIgnore(2, "book", user);
7067
user.addItem(item);
7168

72-
final ObjectMapper mapper = new ObjectMapper();
73-
final String result = mapper.writeValueAsString(item);
69+
final String result = new ObjectMapper().writeValueAsString(item);
7470

7571
assertThat(result, containsString("book"));
7672
assertThat(result, containsString("John"));
7773
assertThat(result, not(containsString("userItems")));
7874
}
7975

8076
@Test
81-
public void givenBidirectionRelation_whenUseCustomSerializer_thenCorrect() throws JsonProcessingException {
77+
public void givenBidirectionRelation_whenUsingCustomSerializer_thenCorrect() throws JsonProcessingException {
8278
final UserWithSerializer user = new UserWithSerializer(1, "John");
8379
final ItemWithSerializer item = new ItemWithSerializer(2, "book", user);
8480
user.addItem(item);
8581

86-
final ObjectMapper mapper = new ObjectMapper();
87-
final String result = mapper.writeValueAsString(item);
82+
final String result = new ObjectMapper().writeValueAsString(item);
8883

8984
assertThat(result, containsString("book"));
9085
assertThat(result, containsString("John"));
9186
assertThat(result, containsString("userItems"));
9287
}
9388

9489
@Test
95-
public void givenBidirectionRelation_whenDeserializeUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
90+
public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
9691
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
9792

98-
final ObjectMapper mapper = new ObjectMapper();
93+
final ItemWithIdentity item = new ObjectMapper().reader(ItemWithIdentity.class).readValue(json);
9994

100-
final ItemWithIdentity item = mapper.reader(ItemWithIdentity.class).readValue(json);
10195
assertEquals(2, item.id);
10296
assertEquals("book", item.itemName);
10397
assertEquals("John", item.owner.name);
10498
}
10599

106100
@Test
107-
public void givenBidirectionRelation_whenUseCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
101+
public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
108102
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
109103

110-
final ObjectMapper mapper = new ObjectMapper();
111-
112-
final ItemWithSerializer item = mapper.reader(ItemWithSerializer.class).readValue(json);
104+
final ItemWithSerializer item = new ObjectMapper().reader(ItemWithSerializer.class).readValue(json);
113105
assertEquals(2, item.id);
114106
assertEquals("book", item.itemName);
115107
assertEquals("John", item.owner.name);

0 commit comments

Comments
 (0)