Giter Site home page Giter Site logo

jersey2-spring-test-example's Introduction

Jersey 2, Spring framework, Mockito test example

The problem I ran into while using Jersey 2 in combination with Spring Framework was, that I was unable to wire in Mockito mocks into Jersey 2 resources while doing integration test. I created this sample application to show you how to solve this problem.

For jersey 2.8 and above you can checkout my solution here

Problem

I want to be able to test a Jersey 2 resource (get status codes, etc...) without creating a complete Spring Application Context.

Goals

The example application has the following goals:

  • Show how to wire a mocked context into resources
  • How gain control over mocked item with Mockito
  • Show you if you're Jersey 2 behaviour is correct

Solution

I've created the following solution.

The SpringContextJerseyTest is a copy of the JerseyTest, the Jersey test didn't allow you to have access to the Spring ApplicationContext. The can be done with SpringContextJerseyTest.getSpringApplicationContext().

I'm able to create mocked objects with Mockito using a factory pattern in the MockFactory. The MockFactory is able to create a mocked version of SomeService.

Testing

You're able to run the test by running this command, or run the test in you're IDE.

mvn test

The real implementation can also be tested, you're able to run the application with the following command.

mvn jetty:run

And when the application is started, you can test the resource with following command (OSX tested only)

curl -i http://localhost:8080/resources/someaction

You should expect a response like this

 HTTP/1.1 204 No Content
 Server: Jetty(8.1.14.v20131031)

jersey2-spring-test-example's People

Contributors

hylke1982 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.