Skip to content

TestingYourService

John Ament edited this page Apr 24, 2017 · 5 revisions

Testing your Hammock Service with Arquillian

Hammock provides an Arquillian extension, starting from the 0.2.0 release, that helps with your integration testing. You should obviously use your integration tests appropriately, following your own testing pyramid, but when it makes sense to use Arquillian and Hammock together, you can use the Hammock Arquillian Extension.

First, add the dependency:

<dependency>
    <groupId>ws.ament.hammock</groupId>
    <artifactId>test-arquillian</artifactId>
    <version>${hammock.version}</version>
    <scope>test</scope>
</dependency>

This will do two things for you:

  • Provide an Arquillian injection point for @ArquillianResource URI uri for your tests.
  • Add the necessary Hammock dependencies to your archive.

This means your archive for testing can be focused on only your needs, and can be as simple as (taken from the Microprofile canonical):

@Deployment
public static JavaArchive archive() {
    return ShrinkWrap.create(JavaArchive.class).addClasses(RestApplication.class, 
         TopCDsEndpoint.class, QLogger.class, ResourceProducer.class);
}

Randomized ports for testing

Starting with the 0.4.0 release, an annotation has been added for tests using Arquillian, @EnableRandomWebServerPort. When this is found, Hammock will launch the webserver on a random port. You can still access the port two different ways - using Arquillian to inject the URI as a resource or injecting the property. The annotation is added at the class level.

Here's an example of using URI injection with RestAssured.

@RunWith(Arquillian.class)
@EnableRandomWebServerPort
public class RestTest {

    @Deployment
    public static JavaArchive createArchive() {
        return ShrinkWrap.create(JavaArchive.class).addClasses(RestController.class, RestApp.class);
    }

    @ArquillianResource
    private URI uri;

    @Test
    public void shouldBeAbleToRetrieveRestEndpoint() throws Exception {
        get(uri + "/rest").then()
                .assertThat().statusCode(200)
                .body(is("Hello, World!"));
    }
}

Likewise, you can inject the port using @Inject @ConfigProperty(name="webserver.port")