Modify jackson date test

This commit is contained in:
DOHA 2014-12-27 19:21:38 +02:00
parent f5eb81e8e6
commit 4efd8ef93a
2 changed files with 40 additions and 1 deletions

View File

@ -44,6 +44,18 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>

View File

@ -17,11 +17,14 @@ import org.baeldung.jackson.date.EventWithJodaTime;
import org.baeldung.jackson.date.EventWithLocalDateTime;
import org.baeldung.jackson.date.EventWithSerializer;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
public class JacksonDateTest {
@ -140,4 +143,28 @@ public class JacksonDateTest {
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
}
}
@Test
public void whenSerializeJava8Date_thenCorrect() throws JsonProcessingException {
final LocalDateTime date = LocalDateTime.of(2014, 12, 20, 2, 30);
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
final String result = mapper.writeValueAsString(date);
assertThat(result, containsString("2014-12-20T02:30"));
}
@Test
public void whenSerializeJodaTime_thenCorrect() throws JsonProcessingException {
final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London"));
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
final String result = mapper.writeValueAsString(date);
assertThat(result, containsString("2014-12-20T02:30:00.000Z"));
}
}