Tidied up tests
This commit is contained in:
parent
7c07b93def
commit
b5985a586c
@ -11,64 +11,51 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class OptionalTypeTest {
|
public class OptionalTypeTest {
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper()
|
ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
|
||||||
.registerModule(new Jdk8Module());
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
|
public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
|
||||||
|
|
||||||
// arrange
|
|
||||||
String subTitle = "The Parish Boy's Progress";
|
String subTitle = "The Parish Boy's Progress";
|
||||||
Book book = new Book();
|
Book book = new Book();
|
||||||
book.setTitle("Oliver Twist");
|
book.setTitle("Oliver Twist");
|
||||||
book.setSubTitle(Optional.of(subTitle));
|
book.setSubTitle(Optional.of(subTitle));
|
||||||
|
|
||||||
// act
|
|
||||||
String result = mapper.writeValueAsString(book);
|
String result = mapper.writeValueAsString(book);
|
||||||
|
|
||||||
// assert
|
|
||||||
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
|
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
|
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
|
||||||
|
|
||||||
// arrange
|
|
||||||
Book book = new Book();
|
Book book = new Book();
|
||||||
book.setTitle("Oliver Twist");
|
book.setTitle("Oliver Twist");
|
||||||
book.setSubTitle(Optional.empty());
|
book.setSubTitle(Optional.empty());
|
||||||
|
|
||||||
// act
|
|
||||||
String result = mapper.writeValueAsString(book);
|
String result = mapper.writeValueAsString(book);
|
||||||
|
|
||||||
// assert
|
|
||||||
assertThat(from(result).getString("subTitle")).isNull();
|
assertThat(from(result).getString("subTitle")).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenField_whenDeserializing_thenOptionalWithValue() throws IOException {
|
public void givenField_whenDeserializingIntoOptional_thenIsPresentWithValue() throws IOException {
|
||||||
|
|
||||||
// arrange
|
|
||||||
String subTitle = "The Parish Boy's Progress";
|
String subTitle = "The Parish Boy's Progress";
|
||||||
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"" + subTitle + "\" }";
|
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"" + subTitle + "\" }";
|
||||||
|
|
||||||
// act
|
|
||||||
Book result = mapper.readValue(book, Book.class);
|
Book result = mapper.readValue(book, Book.class);
|
||||||
|
|
||||||
// assert
|
|
||||||
assertThat(result.getSubTitle()).isEqualTo(Optional.of(subTitle));
|
assertThat(result.getSubTitle()).isEqualTo(Optional.of(subTitle));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmptyField_whenDeserializing_thenEmptyOptional() throws IOException {
|
public void givenNullField_whenDeserializingIntoOptional_thenIsEmpty() throws IOException {
|
||||||
|
|
||||||
// arrange
|
|
||||||
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }";
|
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }";
|
||||||
|
|
||||||
// act
|
|
||||||
Book result = mapper.readValue(book, Book.class);
|
Book result = mapper.readValue(book, Book.class);
|
||||||
|
|
||||||
// assert
|
|
||||||
assertThat(result.getSubTitle()).isEmpty();
|
assertThat(result.getSubTitle()).isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user