Using Payara Embedded as an Arquillian Container Inside IntelliJ IDEA

Uncategorized

With this article, I’m going to integrate Payara Embedded with Arquillian by having it defined inside a sample Maven based application – source code available here – which employs an integration test implemented by the Arquillian framework.  You can also find our previous post about Arquillian and the Payara Server available here, but this time I’ll take it one step further and move onto the IDE side. In this example, I will execute tests just like any JUnit test; meaning the test will be executed directly through the IDE with help of a right-click. I will also configure Payara Embedded as the Arquillian container inside the IDE

Arquillian is an integration and functional testing framework that can be used for testing Java middleware.  You can find more information on what it can be used for on their website here. Intellij IDEA is one of the most powerful IDEs on the market and, with its 2016.2 release, it now offers enhanced support for configuring Arquillian containers.

Setting up Project Structure and Dependencies

My sample project is a web application architected with Maven and it contains a simple domain object, its DAO implementation, its integration test, and the Arquillian configuration.

 

Let’s first start detailing the Maven dependencies. It’s easy to integrate Arquillian via Maven by adding its universal BOM dependency management definition as given follows:

<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>

 

 

Throughout this dependency management section, I will use the arquillian-junit-container dependency by adding it to my project. It provides the integration with JUnit for implementing our tests. It also transitively depends on Arquillian core and ShrinkWrap APIs so I don’t need to add those as separate dependencies.

<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
</dependency> 

The other dependencies that I’m going to add separately are: arquillian-persistence-dbunit, which is a part of the Arquillian persistence extension project that helps with implementing the tests where a persistence layer is involved and arquillian-glassfish-embedded-3.1 dependency, which provides container integration for unit tests and, in our example, it will provide the support for integrating of Payara Embedded into our tests.

<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-dbunit</artifactId>
<version>1.0.0.Alpha7</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>

Since I’ll be using Payara Embedded as a container for running Arquillian tests, I’m  also adding it as a dependency with test scope.

<dependency>
<groupId>fish.payara.extras</groupId>
<artifactId>payara-embedded-all</artifactId>
<version> 4.1.1.163.0.1</version>
<scope>test</scope>
</dependency>

The rest of the dependencies that we have with test scope are JUnit and H2 dependencies. H2 will be used as in-memory database to have the Arquillian test running on. Stay tuned for the connection pool configuration to see the DB configuration in action.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.191</version>
    <scope>test</scope>
</dependency>

 

For integrating Arquillian tests into the Maven’s build process, we also need to define the maven-surefire-plugin with the qualifier configuration for Arquillian as follows.

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemProperties>
            <arquillian.launch>glassfish-embedded</arquillian.launch>
        </systemProperties>
    </configuration>
</plugin>

 

The keyword glassfish-embedded will be defined in Arquillian configuration file, which I’ll be mentioning in a bit.

 

You can see the complete pom.xml here.

 

Setting up Arquillian Configuration

Arquillian can be easily configured via an XML file and a sample configuration for my project is given below. Within the container configuration, I’m providing a GlassFish resource configuration file via a path definition. This GlassFish configuration will contain a JDBC resource and a connection pool definition that will be used by our persistence configuration.

<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<defaultProtocol type="Servlet 3.0"/>
<container qualifier="glassfish-embedded">
<configuration>
<property name="bindHttpPort">7070</property>
<property name="resourcesXml">
src/test/resources/glassfish-resources.xml
</property>
</configuration>
</container>
</arquillian>

 

A sample GlassFish configuration file is shown below. A JDBC resource is created with JNDI name jdbc/payaraEmbedded is created and it’s backed up with the in-memory H2 database configuration.

<!DOCTYPE resources PUBLIC
"-//GlassFish.org//DTD GlassFish Application Server 3.1
Resource Definitions//EN"
"http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-resource pool-name="ArquillianEmbeddedH2Pool"
jndi-name="jdbc/payaraEmbedded"/>
<jdbc-connection-pool name="ArquillianEmbeddedH2Pool"
res-type="javax.sql.DataSource"
datasource-classname="org.h2.jdbcx.JdbcDataSource">
<property name="user" value="sa"/>
<property name="password" value=""/>
<property name="url" value="jdbc:h2:mem:payaraEmbedded"/>
</jdbc-connection-pool>
</resources>

 

Domain model and DAO implementation

To demonstrate the features of Arquillian, I’m going to create a simple domain class named Person, where it contains the properties name and lastName

@Entity
@NamedQueries(
@NamedQuery(name = "Person.getAll",
query = "select p from Person p"))
public class Person {

@Id
@GeneratedValue
private long id;
private String name;
private String lastName;

}

 

The DAO implementation for the Person is also a simple one that just contains the getAll() method for retrieving all persisted data.

@Stateless
public class PersonDao {

@PersistenceContext
private EntityManager entityManager;

public List<Person> getAll() {
return entityManager.createNamedQuery("Person.getAll",
Person.class).getResultList();
}
}

 

Testing the DAO

There is not much to test inside the DAO since it only offers getAll() method but we should always test what we have at hand. To test fetching the all persisted data, an Arquillian test is implemented as follows:

@RunWith(Arquillian.class)
public class PersonDaoTest {

@EJB
private PersonDao personDao;

@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class, "arquillian-example.war")
.addClass(Person.class)
.addClass(PersonDao.class)
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml");
}

@Test
@UsingDataSet("datasets/person.yml")
public void shouldReturnAllPerson() throws Exception {
List<Person> personList = personDao.getAll();

assertNotNull(personList);
assertThat(personList.size(), is(1));
assertThat(personList.get(0).getName(), is("John"));
assertThat(personList.get(0).getLastName(), is("Malkovich"));
}
}

The createDeployment() method creates a micro-deployment that bundles the Person and PersonDao classes and respective persistence configuration file together.

The @UsingDataSet annotation is DBUnit specific which loads the data defined in file person.yml into the database before the test execution. With this approach after invoking the DAO’s getAll() method, I can expect that the list returned not to be null and containing the Person object that I defined with the yaml file. The content of the person.yml is given below. It could also be defined in different formats, such as JSON or XML.

Person:
  - id: 1
    name: "John"
    lastname: "Malkovich"

 

Running the test via Maven

Since I integrated maven-surefire-plugin with Arquillian configuration, running tests is simple as invoking the test phase of Maven lifecycle.

 

Running the test inside IDE

Running the PersonDaoTest inside the IDE is simple as a right-click as seen in the figure below:

 

1_-_Arquillian_IntelliJ_Blog.png

 

But of course some configuration is needed in order to proceed. By clicking Run ‘PersonDaoTest’ from the menu given above, you will come across with the UI in the following screenshot, stating that Arquillian container configuration should be setup first in order to continue.

 

2_-_Arquillian_IntelliJ_Blog.png

 

After clicking + on top left, select Manual container configuration from the menu.

 

3_-_Arquillian_IntelliJ_Blog.png

 

In the dependencies section, click + to add Payara Embedded maven dependency.

 

4_-_Arquillian_IntelliJ_Blog.png

 

Inside the popup, just type fish.payara.extras:payara-embedded-all:4.1.1.163.0.1 into the combo box and select OK.

 

5_-_Arquillian_IntelliJ_Blog.png

 

After getting dependency defined, specify Arquillian.xml container qualifier as glassfish-embedded, which maps to our qualifier configuration defined in the arquillian.xml:

 

6_-_Arquillian_IntelliJ_Blog.png

 

Conclusion

So after getting everything configured, there is only one thing left, which is running the test with just a right-click. With the execution of the test, we will have our JDBC resource and connection pool defined; will have our Person table created in our in-memory H2 database; will have our sample person inserted with the help of @UsingDataSet, and will have that single person which we defined in the yaml file fetched from the DB through our DAO layer.

 

7_-_Arquillian_IntelliJ_Blog.png

Comments (8)

Post a comment

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

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

  1. Ulrich Cech

    Awsome article!

    Is it possible with Payara Micro/Embedded to have something similar to mvn jetty:run? So “build, start und deploy” in one step?

    1. Mert Caliskan

      After getting your artifact built, yes, you can deploy your war onto Payara Micro with a one-liner command line like,

      java -jar payara-micro.jar –deploy test.war

      There is no support for an exploded deploy especially for micro but you can of course bundle payara-embedded to your project and bootstrap it programmatically within your code.

  2. Ulrich Cech

    Yes, I know that command already, but I wanted a solution, which I could execute directly from the IDE via Maven:
    But today, I found the solution:

    1. Add Payara-embedded to the dependencies:

    fish.payara.extras
    payara-embedded-all
    4.1.1.163.0.1

    2. Implement some bootstrapping main class to start the Embedded Payara (taken from another Payara-blogpost):
    public class BootstrapServer {

    public static void main(String[] args) throws GlassFishException {
    GlassFishRuntime runtime = GlassFishRuntime.bootstrap();
    GlassFishProperties gfproperties = new GlassFishProperties();
    gfproperties.setPort(“http-listener”, 8099);
    GlassFish gf = runtime.newGlassFish(gfproperties);
    gf.start();
    gf.getDeployer().deploy(new File(args[0]));
    }
    }

    3. Add the exec-maven-plugin:

    org.codehaus.mojo
    exec-maven-plugin
    1.5.0

    default-cli

    java

    x.y.z.BootstrapServer

    ${project.build.directory}/${project.build.finalName}.${project.packaging}

    4. execute via “clean package exec:java”, and it works like a charm 🙂

    1. Mert Caliskan

      Great! that’s what I mentioned for bootstrapping Payara embedded baked into your application. we can extract another blogpost out of this. thanks.

      1. Ulrich Cech

        I was a bit happy too early. I get an exception if I start the payara embedded this way (even with 163.0.1):

        Caused by: java.lang.IllegalArgumentException: Servlet [RegistrationPortTypeRPCPortImpl] and Servlet [RegistrationRequesterPortTypePortImpl] have the same url pattern: [/RegistrationService_V10]
        at org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl.addWebComponentDescriptor(WebBundleDescriptorImpl.java:362)
        at org.glassfish.webservices.connector.annotation.handlers.WebServiceHandler.processAnnotation(WebServiceHandler.java:461)
        at com.sun.enterprise.deployment.annotation.factory.SJSASFactory$LazyAnnotationHandler.processAnnotation(SJSASFactory.java:148)
        at org.glassfish.apf.impl.AnnotationProcessorImpl.process(AnnotationProcessorImpl.java:352)
        … 43 more

        The same application works fine, if I deploy on Full Payara 163.

        1. Mert Caliskan

          can you open up a ticket on github please? it’d be great if you can share a reproducer also.

  3. Chi

    Awesome article. got one question
    is there anyway for me to bypass the payara embedded all’s hazelcast implement? Our system has the hazelcast 3.7 in the classpath already. so when payara embedded try to deploy, it has errors like this
    Caused by: java.lang.IllegalArgumentException: DataSerializableFactory[0] is already registered! com.hazelcast.internal.cluster.impl.ClusterDataSerializerHook$ClusterDataSerializerFactoryImpl@1a5b7394 -> com.hazelcast.cluster.impl.ClusterDataSerializerHook$ClusterDataSerializerFactoryImpl@26f75d9c
    at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.register(DataSerializableSerializer.java:87)

    Here is part of our pom

    org.jboss.arquillian
    arquillian-bom
    1.1.11.Final
    pom
    import

    javax
    javaee-api
    7.0
    provided

    com.hazelcast
    hazelcast
    ${hazelcast.version}
    provided

    com.hazelcast
    hazelcast-client
    ${hazelcast.version}
    provided

    fish.payara.extras
    payara-embedded-web
    4.1.1.163.0.1

    org.glassfish.main.packager
    hazelcast

    test

    com.h2database
    h2
    ${h2.version}

    org.jboss.arquillian.container
    arquillian-glassfish-embedded-3.1
    1.0.0.Final
    test

    org.jboss.arquillian.junit
    arquillian-junit-container
    test

  4. Atem Nkeng-Asong

    I followed this tutorial yet i get this error:

    Could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor
    java.lang.RuntimeException
    Caused by: java.lang.reflect.InvocationTargetException
    Caused by: java.lang.IllegalArgumentException: No container or group found that match given qualifier: glassfish-embedded

Related Posts

4 minutes
Uncategorized

Leading the Way: Payara Platform Community 7 Beta Now Fully Jakarta EE 11 Certified

We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]

What Is a Java Application Server? A Short Guide 6 minutes
Jakarta EE

What Is a Java Application Server? A Short Guide

Enterprise Java applications power global commerce, healthcare, government and countless other industries. These systems must be scalable, secure and […]

10 minutes
Uncategorized

Java’s 30th Anniversary: A Celebration of Legacy, Evolution and Community

May 2025 marks a monumental milestone in software development: Java turns 30. The impact of this language on the […]