users@jersey.java.net

[Jersey] Re: Unit Testing code that uses the Jersey Client API

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Thu, 08 Dec 2011 11:40:13 +0100

Hi,

I wouldn't try to mock jersey client, it looks like hard thing to do -
it is too complex for this I think.

You have several options how to approach this:

1) using jersey test framework
     you should be able to create (or maybe reuse if you already have
them) resources which could serve similarly as mocking. You might be
able to use in memory container, so it would be fast. But this depends
on the code you want to test - it might not be possible (for example,
you'll need to have the possibility to use client provided by
framework). And additionally, Jersey server (runtime dependencies) and
related dependencies are needed for this to work.

2) hacking jersey client
     it is possible to subclass or modify jersey client to *not*
actually call any rest service, all you need to do is implement your own
TerminatingClientHandler, see [1] for example.

both proposed solutions require some additional code, but I like first
option more..

I would like to know what path are you going to choose, so please keep
us posted :)

Regards,
Pavel


[1]
http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-test-framework/jersey-test-framework-inmemory/src/main/java/com/sun/jersey/test/framework/impl/container/inmemory/TestResourceClientHandler.java


On 12/7/11 7:15 PM, Ronak Patel wrote:
> Hello,
>
> I'm trying to write unit tests for code that uses the Jersey Client
> API. Granted that the main Client class is a class and not an
> interface (and the fact that it is created via a static method) does
> not really aid in mocking it out.
>
> What have people generally done when trying to mock this out?
>
> Do you resort to using cglib to mock the class (instead of asm) and
> then go from there? or is there some sort of framework/API in Jersey
> that aids in this?
>
> Thanks,
>
> Ronak Patel