users@jax-rs-spec.java.net

[jax-rs-spec users] Re: Why is "Entity" class final

From: <wqt.net_at_gmail.com>
Date: Thu, 20 Aug 2015 19:12:13 +0000 (UTC)

Hi Marek. Thanks for replying...

What I really want to stub is the Inovcation.Builder that makes the
actually http call; in unit test, I don't really want it to call HTTP,
so I want to stub it.

The source code looks like this:

// some code to set up the "formPart" parameters...

        final MultiPart multiPart = formPart.bodyPart(new
StreamDataBodyPart("file", inStream));

        final Response response =
invocationBuilder.post(Entity.entity(multiPart,
multiPart.getMediaType()));
        return response.readEntity(JsonObject.class);

Note that "invocationBuilder" is a class field, so in my unit test i
have something like this, using Mockito:

        Invocation.Builder mockBuilder =
mock(Invocation.Builder.class);
        Response mockResponse = mock(Response.class);

// i had to match "entity" to "any()" because "Entity" is final
when(mockBuilder.post((Entity<?>)
Matchers.any())).thenReturn(mockResponse);

        JsonObject expResult = null;
       
when(mockResponse.readEntity(JsonObject.class)).thenReturn(null);

        instance.invocationBuilder = mockBuilder;

        JsonObject result =
instance.retrieveMatchingSidings(matchingCriteria, originalImage);
        assertEquals(expResult, result);

Note that I had to match the "entity" parameter to "any" and downcast
it because "Entity" class is final. Ideally I'd want to give a more
reasonable/verifying stub of "entity" to check my logic; but can not
here due to the "final" mark on Entity.