11package org.baeldung.jackson.serialization;
22
33import java.io.IOException;
4- import java.text.DateFormat;
54import java.text.ParseException;
65import java.text.SimpleDateFormat;
76import java.util.Arrays;
87
98import org.baeldung.jackson.entities.ActorJackson;
109import org.baeldung.jackson.entities.Movie;
1110import org.baeldung.jackson.entities.MovieWithNullValue;
12- import org.baeldung.jackson.serialization.ActorJacksonSerializer;
1311import org.junit.Assert;
1412import org.junit.Test;
1513
16- import com.fasterxml.jackson.core.JsonParseException;
1714import com.fasterxml.jackson.core.JsonProcessingException;
1815import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
19- import com.fasterxml.jackson.databind.JsonMappingException;
2016import com.fasterxml.jackson.databind.ObjectMapper;
2117import com.fasterxml.jackson.databind.SerializationFeature;
2218import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -26,34 +22,34 @@ public class JacksonSerializeTest {
2622 @Test
2723 public void whenSimpleSerialize_thenCorrect() throws JsonProcessingException, ParseException {
2824
29- SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
25+ final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
3026
31- ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
32- Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
27+ final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
28+ final Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
3329
34- ObjectMapper mapper = new ObjectMapper();
35- String jsonResult = mapper.writeValueAsString(movie);
30+ final ObjectMapper mapper = new ObjectMapper();
31+ final String jsonResult = mapper.writeValueAsString(movie);
3632
37- String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401439600000 ,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
33+ final String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401403600000 ,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
3834 Assert.assertEquals(jsonResult, expectedOutput);
3935 }
4036
4137 @Test
4238 public void whenCustomSerialize_thenCorrect() throws ParseException, IOException {
4339
44- SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
40+ final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
4541
46- ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
47- MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
42+ final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
43+ final MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
4844
49- SimpleModule module = new SimpleModule();
45+ final SimpleModule module = new SimpleModule();
5046 module.addSerializer(new ActorJacksonSerializer(ActorJackson.class));
51- ObjectMapper mapper = new ObjectMapper();
47+ final ObjectMapper mapper = new ObjectMapper();
5248
53- String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
49+ final String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
5450
55- Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
56- String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
51+ final Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
52+ final String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
5753
5854 Assert.assertEquals(jsonResult, expectedOutput);
5955 }
0 commit comments