DropwizardAppRule throws: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.time.LocalDate out of START_ARRAY token

1,163 views
Skip to first unread message

Antti Pohjanpalo

unread,
Sep 27, 2017, 12:57:00 PM9/27/17
to dropwizard-user
Hi!

I'm having problem with the following test class:

public class NotesApplicationTest {

   
@ClassRule
   
public static final DropwizardAppRule<NotesConfiguration> APP = new DropwizardAppRule<>(
           
NotesApplication.class,
           
ResourceHelpers.resourceFilePath("test-config.yml")
   
);

   
private final String BASE_URI = String.format("http://localhost:%d", APP.getLocalPort());

   
@Test
   
public void shouldGetNotes() {
       
Response response = APP.client()
               
.target(BASE_URI + "/notes/1")
               
.request(MediaType.APPLICATION_JSON_TYPE)
               
.get();

        assertThat
(response.getStatus()).isEqualTo(200);
       
NoteResponse note = response.readEntity(NoteResponse.class); // this line throws error
   
}

}

The error I get is this:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.time.LocalDate out of START_ARRAY token
 at
[Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@28b286c0; line: 1, column: 16] (through reference chain: anttipp.notes.rest.api.NoteResponse["date"])
 at com
.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
 at com
.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)

The application itself works fine and testing with ResourceTestRule is also working fine. I'm using Dropwizard 1.1.4. How should I setup my test case to get it working? Could this be something similar than in https://github.com/dropwizard/dropwizard-java8/issues/8 ?

Antti Pohjanpalo

unread,
Sep 29, 2017, 3:51:11 PM9/29/17
to dropwizard-user
I managed to get it working by adding:

@Before
public void before() {
   
ObjectMapper objectMapper = new ObjectMapper();
    objectMapper
.registerModule(new JavaTimeModule());

   
JacksonJsonProvider provider = new JacksonJsonProvider();
    provider
.setMapper(objectMapper);

    APP
.client().register(provider);
}

Not sure if I should open an issue for this or not.

Antti Pohjanpalo

unread,
Sep 30, 2017, 3:29:42 AM9/30/17
to dropwizard-user
I faced other deserialization problems. Instead of creating new ObjectMapper I overrided method io.dropwizard.testing.junit.DropwizardAppRule#clientBuilder and used io.dropwizard.testing.junit.DropwizardAppRule#getObjectMapper:

@ClassRule
public static final DropwizardAppRule<NotesConfiguration> APP = new DropwizardAppRule<NotesConfiguration>(
       
NotesApplication.class,
       
ResourceHelpers.resourceFilePath("test-config.yml")
) {
   
@Override
   
protected JerseyClientBuilder clientBuilder() {
       
final JacksonJsonProvider jsonProvider = new JacksonJsonProvider();
        jsonProvider
.setMapper(getObjectMapper());

       
return new JerseyClientBuilder()
               
.property(ClientProperties.CONNECT_TIMEOUT, 1000)
               
.property(ClientProperties.READ_TIMEOUT, 5000)
               
.register(jsonProvider);
   
}
};
Reply all
Reply to author
Forward
0 new messages