I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

1. Overview

In this tutorial, we’re going to take a look at the Jersey Test Framework and see how we can use it to quickly write integration tests.

As we’ve already seen in previous articles, Jersey is an open source framework for developing RESTful Web Services. We can learn more about Jersey in our introduction to creating an API with Jersey and Spring article – here.

2. Application Setup

The Jersey Test Framework is a tool to help us verify the correct implementation of our server-side components. As we’ll see later, it provides a fast and fuss-free way for writing integration tests and can handle communicating with our HTTP APIs very well.

Likewise, it works almost out-of-the-box and it’s easy to integrate with our Maven-based projects. The framework is primarily based on JUnit although it is possible to use with TestNG as well which makes it usable in almost all environments.

In the next section, we’ll see which dependencies we need to add to our application in order to use the framework.

Notice that we have full access to the HTTP response – so we can do things like checking the status code to make sure the operation was actually successful, or work with the actual body of the response.

Let’s explain in more detail what we do in the above example:

Send an HTTP GET request to ‘/greetings/hi’

Check the HTTP status code and content type response headers

Test the contents of the response contains the string “hi”

4. Testing GET to Retrieve Resources

Now, that we’ve seen the basic steps involved in creating tests. Let’s test the simple Fruit API that we introduced in the excellent Jersey MVC Support article.

4.1. Get Plain JSON

In the below example we’re working with the response body as a standard JSON String:

When we create and configure our Jersey application under test. We can also enable additional properties. In this case, we enable two logging properties – LOG_TRAFFIC and DUMP_ENTITYwhich will provide useful additional logging and debug information during test runs.

8. Supported Containers

As we’ve already mentioned the defacto container used when writing tests with the Jersey Test Framework is Grizzly. However, a number of other containers are supported:

In-Memory container

HttpServer from Oracle JDK

Simple container (org.simpleframework.http

Jetty container (org.eclipse.jetty)

For more information on how to configure these containers, please see the documentation here.

9. Conclusion

To summarize, in this tutorial, we’ve explored the Jersey Test Framework. First, we started by introducing how to configure the Jersey Test Framework and then we saw how to write a test for a very simple API.

In the next section, we saw how to write tests for a variety of GET and POST API endpoints. Finally, we looked at some additional features and the containers that the Jersey Test Framework supports.

As always, the full source code of the article is available over on GitHub.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: