3

I would like to setup a very lightweight jersey unit-test. My webservice produces JSON result which I want to validate directly without using a HTTP-Server. Is it possible to create lighweight jsersey tests without using a HTTP-Server like Grizzly HTTP?

cctest cc
  • 33
  • 3

2 Answers2

3

Not sure what version of Jersey you are using, but look into Jersey Test Framework. There is an in-memory container, that doesn't involve any network connection. Here's a documentation

You can see some different examples in the project source code tests

Dependencies

  • Jersey 2.x

    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-inmemory</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    
  • Jersey 1.x

    <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-inmemory</artifactId>
        <version>${jersey1.version}</version>
    </dependency>
    

The only down side, is that if you have any dependency on some specific Servlet functionality, you may not get it here.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
-1

Just take a look for Jersey Test Framework. You can in a simple way create tests for your web services. If you really need strict unit tests you should just create an instance of your resource class, mock any dependencies and do some assertions. Show an example code.

Dawid Stępień
  • 1,357
  • 1
  • 9
  • 6