Buy Support Online

Testing applications with Payara Server & Arquillian

07 Mar 2016

Before putting any application out for public access, it’s always worth testing that it works how you expect it to (and also how you don’t expect it to!). Effective testing of applications is something you can spend a week being taught, but for this blog I’ll just cover the basics of running JUnit and TestNG tests against Payara Server using Arquillian.

 

 

Arquillian is a testing platform that can be used to perform unit, functional, and integration tests of applications being deployed to Java Application Servers. You can find more information on what it can be used for on their website here.

 

Setting Up

Arquillian is probably easiest to integrate into your projects using Maven, a build dependency tool used in many projects – Payara Server included!

In your Maven POM, include the following dependencies, excluding the JUnit or TestNG dependency as per your preference or requirements:

 

<dependencyManagement>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.11.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        ...
    </dependencies>
    ...
</dependencyManagement>

<dependencies>
    ...
    <dependency>
        <groupId>org.jboss.arquillian.testng</groupId>
        <artifactId>arquillian-testng-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.10</version>
        <scope>test</scope>
    </dependency>
    ...
</dependencies>

 

If you’re not using Maven though, you can still find and download the artefact libraries from Maven Central manually here.

 

If you don’t already have it included in your tests, you’ll also need to include the Java EE API in your dependencies. This can have its scope set to provided, as Payara Server will have the required implementation:

 

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

 

The last thing to set up is your container, namely Payara Server. Include the following dependencies in your POM for testing against an Embedded Payara Server instance:

 

<dependency>
    <groupId>org.jboss.arquillian.container</groupId>
    <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
    <version>1.0.0.CR4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>fish.payara.extras</groupId>
    <artifactId>payara-embedded-all</artifactId>
    <version>4.1.1.161</version>
    <scope>test</scope>
</dependency>

 

Or instead just include the following dependency if you want to test your applications against a remote Payara Server instance (as in, a standalone Payara Server instance that you’ve started yourself):

 

<dependency>
    <groupId>org.jboss.arquillian.container</groupId>
    <artifactId>arquillian-glassfish-remote-3.1</artifactId>
    <version>1.0.0.CR4</version>
    <scope>test</scope>
</dependency>

 

 

Running your Tests against Payara Server

 

To run tests with Arquillian against Payara Server, test classes must be annotated with @RunWith(Arquillian.class) or extend the Arquillian class (public class x extends Arquillian) for JUnit or TestNG tests respectively, and have a method annotated with @Deployment to create and deploy a test archive.

 

Arquillian uses test archives to specify the classes and resources required for the test to deploy and run. These test archives use the ShrinkWrap API to define a JAR, WAR, or EAR deployment, allowing your tests to range from basic unit tests, to complicated integration and functional tests.

 

Then you just need to annotate your test methods with @Test or @Test() for JUnit or TestNG respectively, and run them!

 

An Example

To help put things into context, I’ll demonstrate a very basic test using TestNG, Maven, and embedded Payara Server (I’ll assume you already know how to create a Maven project, otherwise check out Maven’s documentation: https://maven.apache.org/guides/getting-started/). First, fill out your POM with the required dependencies I described before so it looks like this:

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fish.payara</groupId>
    <artifactId>arquillian-example</artifactId>
    <version>1</version>
    <packaging>jar</packaging>

    <name>ArquillianExample</name>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.1.11.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.testng</groupId>
            <artifactId>arquillian-testng-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.container</groupId>
            <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
            <version>1.0.0.CR4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>fish.payara.extras</groupId>
            <artifactId>payara-embedded-all</artifactId>
            <version>4.1.1.161</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
</project>

 

Next, you need an application to test. Below is a basic application containing just a single method:

 

public class HelloWorld {
    public String sayHello() {
        return "Hello World";
    }
}

 

Then, you need a test class. This test class simply creates the deployment for Arquillian, and checks that the message being returned by the sayHello() method is correct:

 

import fish.payara.arquillianexample.HelloWorld;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.testng.Assert;
import org.testng.annotations.Test;

public class HelloWorldTest extends Arquillian {
    
    @Deployment
    public static JavaArchive createDeployment() {
        return ShrinkWrap.create(JavaArchive.class)
            .addClass(HelloWorld.class);
    }

    @Test()
    public void sayHelloTest() {
        HelloWorld helloWorld = new HelloWorld();
        Assert.assertEquals(helloWorld.sayHello(), "Hello World");
    }
}

 

 

And finally, you can then run the test with one command: mvn clean test

This will compile your application and test, before starting an embedded Payara Server instance, deploying it, and running the test method.

 

Wrapping Up

Hopefully this should give you enough of a start to begin running your own tests against Payara Server. If you still need some extra help though, the Arquillian website hosts some decent guides which covers what I have, and then some.

 

 

 

Comments

Subscribe

Themes

see all