Created
March 23, 2018 18:18
-
-
Save pq-yilinwei/2e3ea4b547cfa0fb7de50c27c78c207a to your computer and use it in GitHub Desktop.
OptionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fasterxml.jackson; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import java.util.Optional; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
public class OptionalTest { | |
static final class Foo { | |
private final Integer bar; | |
private final String moo; | |
Foo(Integer bar, String moo) { | |
this.bar = bar; | |
this.moo = moo; | |
} | |
public Optional<Integer> getBar() { | |
return Optional.ofNullable(bar); | |
} | |
public String getMoo() { | |
return moo; | |
} | |
} | |
private ObjectMapper objectMapper; | |
@BeforeEach | |
void setup() { | |
objectMapper = new ObjectMapper() | |
.setSerializationInclusion(JsonInclude.Include.NON_NULL) | |
.registerModule(new Jdk8Module()); | |
} | |
@Test | |
void testOptionalPresentExists() throws JsonProcessingException { | |
assertEquals("{\"bar\":3}", objectMapper.writeValueAsString(new Foo(1, null))); | |
} | |
@Test | |
void testOptionalEmptyDoesNotExist() throws JsonProcessingException { | |
assertEquals("{}", objectMapper.writeValueAsString(new Foo(null, null))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment