RSSD RSSD - 1 year ago 146
Java Question

JAXRS + JerseyTest testing a REST Service

I've created a Rest service with four methods, GET,POST,UPDATE and DELETE.
These methods make connections to a Database to retrieve and store data.

Now I want to test each method. I've used the Jersey Test Framework for this. And it is working as long as I remove the code what actually makes the call to the database. When I leave the code that makes the call to the database it throws an exception that it could not connect to the database.

EDIT: I have done some research and used dependancy injection. The db calls are moved to a separate class but I'm still doing something wrong.

DatabaseResults. In this class the call to the DB is made.

public class DatabaseResults {

private final String getQuery = "SELECT * FROM movies";
private Connection connection = null;
private PreparedStatement pstmt = null;
private final ArrayList<Movie> jsonList = new ArrayList<>();

public JSONObject getAllMovies() throws SQLException {

try {
ComboPooledDataSource dataSource = DatabaseUtility.getDataSource();
connection = dataSource.getConnection();

pstmt = connection.prepareStatement(getQuery);
ResultSet rs = pstmt.executeQuery();

while ( {
jsonList.add(new Movie(rs.getString(1), rs.getString(2), rs.getString(4), rs.getString(3)));

} catch (SQLException ex) {
System.out.println("Could not retrieve a connection");
} finally {

JSONObject jsonObject = new JSONObject();
jsonObject.put("movies", jsonList);

return jsonObject;

MoviesResource that contains the REST methods

public class MoviesResource {

private DatabaseResults dbResults = null;

public MoviesResource() {
this(new DatabaseResults());

MoviesResource(DatabaseResults dbr){
this.dbResults = dbr;

public Response getAllMovies() throws JSONException, SQLException {

return Response.status(200).entity(dbResults.getAllMovies().toString()).build();

The Test class

public class MovieResourceTest extends JerseyTest {

JSONObject jsonObject = new JSONObject();

DatabaseResults dbr;

public void setup() throws SQLException{
jsonObject.put("id", "hello");


Client client = ClientBuilder.newClient();
WebTarget target = client

protected Application configure() {
return new ResourceConfig(MoviesResource.class);

public void getAllMoviesTest() throws SQLException {

String responseGetAllMovies = target("/movies").request().get(String.class);


At this moment I can run the tests but still when I test the
method it makes a call to the real database instead of returning the

I have the feeling that a connection is missing between the mock object and the constructor from the MovieResource class?

Answer Source

When you register your resource as a class

new ResourceConfig(MoviesResource.class)

you are telling Jersey to create the instance. If you don't have any DI configured, it will just call the no-arg constructor. In your no-arg constructor, you are just creating the service yourself. It knows nothing about your mock.

What you should do instead is register the resource class as an instance. That way you can pass the mock to the constructor.

return new ResourceConfig()
        .register(new MoviesResource(dbr));

Don't use the Mockito runner. Instead use the MockitoAnnotations.initMocks method. That way you control when the @Mocks are injected. If you use the runner, the injection will not happen in time, as the the configure method is called by the framework before the Mockito injection happens.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download