Tidied up tests
This commit is contained in:
parent
7c07b93def
commit
b5985a586c
|
@ -11,64 +11,51 @@ import org.junit.Test;
|
|||
|
||||
public class OptionalTypeTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper()
|
||||
.registerModule(new Jdk8Module());
|
||||
ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
|
||||
public void givenPresentOptional_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 {
|
||||
public void givenField_whenDeserializingIntoOptional_thenIsPresentWithValue() 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 {
|
||||
public void givenNullField_whenDeserializingIntoOptional_thenIsEmpty() throws IOException {
|
||||
|
||||
// arrange
|
||||
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }";
|
||||
|
||||
// act
|
||||
Book result = mapper.readValue(book, Book.class);
|
||||
|
||||
// assert
|
||||
assertThat(result.getSubTitle()).isEmpty();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue