users@jersey.java.net

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

From: Ronak Patel <ronak2121_at_yahoo.com>
Date: Thu, 8 Dec 2011 06:28:05 -0800 (PST)

Hi Pavel,

Setting up and using the Jersey Test Framework seems more like an integration test to me rather than a unit test.
Same thing goes for using it to bring up a resource and then using the Jersey client to make REST calls.

They will require having databases set up with test data and so on just to make one unit test pass....
That seems rather excessive for a unit test (but totally okay for an integration test).

The Client class is not final and has a default constructor, so it seems cglib/EasyMock would work here.
I'll try it out and let you know how that goes.

Ronak


________________________________
 From: Pavel Bucek <pavel.bucek_at_oracle.com>
To: users_at_jersey.java.net
Sent: Thursday, December 8, 2011 5:40 AM
Subject: [Jersey] Re: Unit Testing code that uses the Jersey Client API
 

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