REST Test – Add Integration Tests

Writing the integration test code proved simpler than the unit tests, but getting them running wasn’t.

First, let’s look at the tests. They are implemented using restfuse (a JUnit extension for automated REST service testing).

One other small item to highlight is the regular expression Hamcrest matcher; the static import here being nothing more than a builder wrapper for Piotr Gabryanczyk’s implementation.

package com.nigeleke.restin.service;

import com.eclipsesource.restfuse.Destination;
import com.eclipsesource.restfuse.HttpJUnitRunner;
import com.eclipsesource.restfuse.MediaType;
import com.eclipsesource.restfuse.Method;
import com.eclipsesource.restfuse.Response;
import com.eclipsesource.restfuse.annotation.Context;
import com.eclipsesource.restfuse.annotation.HttpTest;
import java.util.List;
import org.junit.Rule;
import org.junit.runner.RunWith;

import static com.eclipsesource.restfuse.Assert.*;
import static com.nigeleke.tuttle.hamcrest.Matchers.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class AnEntityFacadeRESTIT {

  public Destination destination = new Destination(this, "http://localhost:21957/restin/rest");

  private Response response;
  @HttpTest(method = Method.POST, path = "/anentity", type = MediaType.APPLICATION_JSON, content = "{ \"name\":\"Arthur C Clarke\" }")
  public void testCreateJson() throws Exception {

    assertThat(response.getType(), equalTo(MediaType.APPLICATION_JSON));
    assertThat(response.getBody(), matchesRegex("^\\{\\\"id\\\":\\d+,\\\"name\\\":\\\"Arthur C Clarke\\\"}$"));
    assertThat(response.getUrl(), matchesRegex("^http://localhost:21957/restin/rest/anentity$"));

    List<String> contentLocations = response.getHeaders().get(HttpHeaders.CONTENT_LOCATION);
    assertEquals(1, contentLocations.size());
    assertThat(contentLocations.iterator().next(), matchesRegex("^http://localhost:21957/restin/rest/anentity/\\d+$"));

Embedded Glassfish was used as the test container, along with Embedded Derby for the test database.

The “more interesting” changes were in the pom.xml. In addition to the restfuse dependency already stated, the embedded Glassfish maven plugin was incorporated, along with a Derby server plugin.

First, the pom.xml additions for Glassfish were:

      <command>add-resources ${basedir}/src/test/setup/glassfish-resources.xml</command>

Notice the command add-resources. This sets up the test JNDI reference to an embedded derby database.

Before using add-resources I was getting an error message for a defaulted persistence unit name restin__pm. There are many posts around this exception, but the solutions weren’t working for me. The glassfish-resources solution was the solution that finally worked – and hence the reason for sharing this on the post.

The resource glassfish-resources.xml is placed under the test resources folder and contains:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-// GlassFish Application Server 3.1 Resource Definitions//EN" "">
  <jdbc-connection-pool name="derby_net_restin_APPPool"
    res-type="javax.sql.DataSource" ping="true">
    <property name="databaseName" value="memory:restin" />
    <property name="connectionAttributes" value=";create=true" />
    <property name="user" value="APP" />
    <property name="password" value="APP" />
    <property name="driverClass" value="org.apache.derby.jdbc.EmbeddedDriver" />
  <jdbc-resource jndi-name="restin" pool-name="derby_net_restin_APPPool" object-type="user" enabled="true" />

Back to the pom.xml, and the Derby servers is started and stopped with:


The MAVEN_OPTS environment variable needed to be set to get the integration tests to run successfylly.

export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m"

REST Test - Unit Tests and Coverage Reporting REST Test - Continuous Integration

5 thoughts on “REST Test – Add Integration Tests

  1. Hi!

    Nice tutorial! Thanks for illustrating how to use my plugin (org.carlspring.maven:derby-maven-plugin)! Glad to see people are finding it useful! 🙂

    I’ve linked back to this page, as I’m sure other of people will find it interesting.



  2. Hello Nigel,

    at first I’d like to thank you for excellent tutorial. You helped me a lot! I got stuck on that point (probably because I’m quite new to derby and embedded glassfish). It would be nice if you could help me.

    I’m doing exactly what you described in tutorial, but I faced:

    WARNING: The web application [unknown] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
    mar 04, 2016 11:30:55 PM org.glassfish.deployment.admin.DeployCommand execute
    SEVERE: Exception while preparing the app : Invalid resource : java:/jacekDS__pm
    com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : java:/jacekDS__pm

    I can mention that glassfish-resources.xml was loaded properly.

    Exit Code [SUCCESS]
    JDBC connection pool jdbc/myapp_pool created successfully.
    Attribute value (pool-name = jdbc/swalkU) is not found in list of jdbc connection pools.


    1. Exit Code [SUCCESS]
      JDBC connection pool derby_net_jacek_APPPool created successfully.
      JDBC resource jacekcreated successfully.

      But anyway I’m still getting SEVERE: Exception while preparing the app : Invalid resource : java:/SwalkDS__pm.

  3. I /suspect/ you have a Glassfish configuration issue. I recall some problems using correct database config references and seeing the __pm when I didn’t expect it – unfortunately too long ago to recall details.

    I’ve have dug up the old project, updated Netbeans, pom dependencies etc and am currently rerunning. As of tonight I’ve updated the example to use the MOXy JsonProvider, but currently having an authorisation error connecting to the database. Which all means that I haven’t recreated your problem yet – but nor have I been able to rerun my example in my updated dev environment.

    I’ll continue to investigate… watch this space.

  4. I’ve amended the user/password authentication settings, and have no other problems. The example works for unit test, integration test and deployment. This was updated to run under Glassfish 4.1.1.

    I’ve posted the code on github at, so you can browse the full example.

    I believe the __pm issue will be a combination of your Persistence Unit, JNDI and Connection Pool settings – but that’s a bit of a guess.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.