Jersey Unit Tests without HTTP Server

旧时模样 提交于 2019-12-10 19:29:45

问题


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?


回答1:


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

  • Jersey 2.x Test Framework
  • Jersey 1.x Test Framework

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

  • Jersey 2
  • Jersey 1

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.




回答2:


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.



来源:https://stackoverflow.com/questions/29624974/jersey-unit-tests-without-http-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!