Test Serialize/Deserialize Optional with Jackson

This commit is contained in:
Luís Soares 2017-02-05 18:21:52 +00:00 committed by Andrew Morgan
parent 5218116a80
commit ec2bfa7014
3 changed files with 109 additions and 4 deletions

View File

@ -67,6 +67,12 @@
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
@ -128,8 +134,8 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<!-- logging -->
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
@ -152,7 +158,7 @@
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
</dependency>
</dependencies>
@ -198,7 +204,7 @@
<!-- util -->
<guava.version>19.0</guava.version>
<commons-lang3.version>3.5</commons-lang3.version>
<commons-io.version>2.5</commons-io.version>
<commons-io.version>2.5</commons-io.version>
<joda-time.version>2.9.6</joda-time.version>
<gson.version>2.8.0</gson.version>
<commons-collections4.version>4.1</commons-collections4.version>

View File

@ -0,0 +1,25 @@
package com.baeldung.jackson.miscellaneous.mixin;
import java.util.Optional;
public class Book {
private String title;
private Optional<String> subTitle;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Optional<String> getSubTitle() {
return subTitle;
}
public void setSubTitle(Optional<String> subTitle) {
this.subTitle = subTitle;
}
}

View File

@ -0,0 +1,74 @@
package com.baeldung.jackson.miscellaneous.mixin;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import static io.restassured.path.json.JsonPath.from;
import java.io.IOException;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class OptionalTypeTest {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new Jdk8Module());
@Test
public void givenOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
// arrange
String subTitle = "The Parish Boy's Progress";
Book book = new Book();
book.setTitle("Oliver Twist");
book.setSubTitle(Optional.of(subTitle));
// act
String result = mapper.writeValueAsString(book);
// assert
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
}
@Test
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
// arrange
Book book = new Book();
book.setTitle("Oliver Twist");
book.setSubTitle(Optional.empty());
// act
String result = mapper.writeValueAsString(book);
// assert
assertThat(from(result).getString("subTitle")).isNull();
}
@Test
public void givenField_whenDeserializing_thenOptionalWithValue() throws IOException {
// arrange
String subTitle = "The Parish Boy's Progress";
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"" + subTitle + "\" }";
// act
Book result = mapper.readValue(book, Book.class);
// assert
assertThat(result.getSubTitle()).isEqualTo(Optional.of(subTitle));
}
@Test
public void givenEmptyField_whenDeserializing_thenEmptyOptional() throws IOException {
// arrange
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }";
// act
Book result = mapper.readValue(book, Book.class);
// assert
assertThat(result.getSubTitle()).isEmpty();
}
}