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:
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.
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.
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.
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.
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.
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.
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.
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
The DAO implementation for the
Person is also a simple one that just contains the
getAll() method for retrieving all persisted data.
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:
createDeployment() method creates a micro-deployment that bundles the
PersonDao classes and respective persistence configuration file together.
@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.
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:
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.
After clicking + on top left, select Manual container configuration from the menu.
In the dependencies section, click + to add Payara Embedded maven dependency.
Inside the popup, just type fish.payara.extras:payara-embedded-all:22.214.171.124.0.1 into the combo box and select OK.
After getting dependency defined, specify Arquillian.xml container qualifier as
glassfish-embedded, which maps to our qualifier configuration defined in the
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.