How can I make my entity marhsall to JSON in JerseyTest?

431 Views Asked by At

I have some test code that looks like this...

    @Test
    public void testCreateWasteApprovalApplication() {
        final ManualWasteApprovalApplicationDto dto = new ManualWasteApprovalApplicationDto();
        dto.setDate(Instant.now().getMillis());
        dto.setWasteGeneratorName("wasteGeneratorName");
//... set a bunch more properties
        dto.setFacilityGroup("facgroup12");
        final Entity<ManualWasteApprovalApplicationDto> entity = Entity.entity(dto, MediaType.APPLICATION_JSON_TYPE);
        Response response = target("wasteapproval/1").request(MediaType.APPLICATION_JSON).put(entity);
        assertNotNull(response);
    }

When I try to run this inside Jersey Test I see the following stack trace.

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.tervita.portal.wasteapproval.controller.ManualWasteApprovalApplicationDto, genericType=class com.tervita.portal.wasteapproval.controller.ManualWasteApprovalApplicationDto.

    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:227)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:495)
    at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:270)
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:182)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:227)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:655)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:652)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:652)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:412)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.put(JerseyInvocation.java:306)
    at com.tervita.portal.wasteapproval.service.WasteApprovalApplicationControllerTest.testCreateWasteApprovalApplication(WasteApprovalApplicationControllerTest.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

Is this a valid technique to marshal a java object to json when running a jersey test? Do I have to register a JSON marshaller of some kind?

Them method I am calling in my existing Jersey service has this method signature...

@POST
@Path("{id}")
public ManualWasteApprovalApplication updateApplication(@PathParam("id") Long id, ManualWasteApprovalApplicationDto dto) {
1

There are 1 best solutions below

0
On BEST ANSWER

If you don't already have it, you need a JSON provider. Just use Jackson.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

This needs to be registered with both the server and the client. If you are using Jersey version newer than 2.8 (i.e. 2.9+), then you don't need to register anything, it should automatically get registered. If you are using an older version, then you need to register the JacksonFeature.

For the client

@Override
public void configureClient(ClientConfig config) {
    config.register(JacksonFeature.class);
}

For server

@Override
public Application configure() {
    return new ResourceConfig(...).register(JacksonFeature.class);
}

If you're not using Maven, have a look at this post