Application Testing with Arquillian Persistence Extension
madoar Nov 5, 2016 5:44 PMHello everyone,
I'm currently trying to write integration tests for a rest application of mine.
This application depends on a background database which it accesses through JPA.
The application server I use is wildfly 10.
The application I wrote with a mix of java and scala.
My maven pom file looks like followed:
<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>
<parent>
<groupId>info.armado.darmstadtspielt</groupId>
<artifactId>ausleihe-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ausleihe-rest-server</artifactId>
<packaging>war</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.11.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-bom</artifactId>
<version>2.2.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-transaction-bom</artifactId>
<version>1.0.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>info.armado.darmstadtspielt</groupId>
<artifactId>ausleihe-database</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
<type>ejb</type>
</dependency>
<dependency>
<groupId>info.armado.darmstadtspielt</groupId>
<artifactId>ausleihe-rest-interfaces</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-all-7.0</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-embedded</artifactId>
<version>2.0.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-transaction-jta</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-client-api</artifactId>
<version>1.0.0.Alpha4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-client-impl-3x</artifactId>
<version>1.0.0.Alpha4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>3.0.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-api</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-core</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<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.extension</groupId>
<artifactId>arquillian-persistence-spi</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<!-- Run scala compiler in the process-resources phase, so that dependencies
on scala classes can be resolved later in the (Java) compile phase -->
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<!-- Run scala compiler in the process-test-resources phase, so that
dependencies on scala classes can be resolved later in the (Java) test-compile
phase -->
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<!-- Add src/main/scala to source path of Eclipse -->
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
</sources>
</configuration>
</execution>
<!-- Add src/test/scala to test source path of Eclipse -->
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- to generate Eclipse artifacts for projects mixing Scala and Java -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<projectnatures>
<projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
</projectnatures>
<buildcommands>
<buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
</buildcommands>
<classpathContainers>
<classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
</classpathContainers>
<excludes>
<!-- in Eclipse, use scala-library, scala-compiler from the SCALA_CONTAINER
rather than POM <dependency> -->
<exclude>org.scala-lang:scala-library</exclude>
<exclude>org.scala-lang:scala-compiler</exclude>
</excludes>
<sourceIncludes>
<sourceInclude>**/*.scala</sourceInclude>
<sourceInclude>**/*.java</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
<!-- You need the maven dependency plugin to download locally a zip with
the server, unless you provide your own, it will download under the /target
directory -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-dist</artifactId>
<version>10.1.0.Final</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>target</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- When run tests in the test phase, include .java and .scala source
files -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<includes>
<include>**/*.java</include>
<include>**/*.scala</include>
</includes>
<!-- Fork every test because it will launch a separate AS instance -->
<forkMode>always</forkMode>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
Normally I deploy this rest endpoint war application as a part of an ear archive which also contains a graphical web interface, but for testing this isn't needed.
My aquillian.xml file has the following content:
<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">
<container qualifier="wildfly10" default="true">
<configuration>
<property name="jbossHome">target/wildfly-10.1.0.Final/</property>
<property name="modulePath">target/wildfly-10.1.0.Final/modules</property>
</configuration>
</container>
<extension qualifier="transaction">
<property name="manager">java:jboss/UserTransaction</property>
</extension>
<extension qualifier="persistence-script">
<property name="scriptsToExecuteBeforeTest">SET REFERENTIAL_INTEGRITY FALSE;</property>
<property name="scriptsToExecuteAfterTest">SET REFERENTIAL_INTEGRITY TRUE;</property>
</extension>
<extension qualifier="persistence-dbunit">
<property name="datatypeFactory">org.dbunit.ext.h2.H2DataTypeFactory</property>
</extension>
</arquillian>
My test-persistence.xml file has the following content:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ausleihe" transaction-type="JTA">
<!-- Provider Informations -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/ausleihe</jta-data-source>
<!-- Properties -->
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
My wildfly-ds.xml file has the following content:
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.org/ironjacamar/schema
http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<datasource enabled="true" jta="true"
jndi-name="java:jboss/datasources/ausleihe"
pool-name="ArquillianEmbeddedH2Pool">
<connection-url>jdbc:h2:mem:ausleihe;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
</datasource>
</datasources>
My tests look like the following:
object UniversalTest {
@Deployment
def createDeployment: WebArchive = {
val result = ShrinkWrap
.create(classOf[WebArchive])
.addPackages(true, "info.armado.ausleihe")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsResource("full.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("wildfly-ds.xml")
.addAsLibraries {
Maven
.resolver
.resolve("org.scala-lang:scala-library:2.11.8")
.withTransitivity
.asFile: _*
}
.addAsLibraries {
Maven
.resolver
.resolve("org.scalatest:scalatest_2.11:3.0.0")
.withTransitivity
.asFile: _*
}
result
}
}
@RunWith(classOf[Arquillian])
class UniversalTest extends JUnitSuite {
@Inject
var universalService: Universal = _
@Test
@UsingDataSet(Array("full.xml"))
def gamesBarcodeInUse(): Unit = {
// test if an activated game can be correctly found
universalService.barcodeInUse("11000014") should equal(LendingEntityInUse(GameData("11000014", "Titel 1", "Autor 1", "Verlag 1"), GameInUse(IdentityCardData("33000010", "Marc Arndt"), EnvelopeData("44000013"))))
universalService.barcodeInUse("11000058") should equal(LendingEntityInUse(GameData("11000058", "Titel 4", "Autor 3", "Verlag 2"), NotInUse()))
// test if an not activated game can't be found
universalService.barcodeInUse("11000070") should equal(LendingEntityNotExists("11000070"))
// test if a not existing game can't be found
universalService.barcodeInUse("11000081") should equal(LendingEntityNotExists("11000081"))
}
@Test
@UsingDataSet(Array("full.xml"))
def identityCardBarcodeInUse(): Unit = {
universalService.barcodeInUse("33000010") should equal(LendingEntityInUse(IdentityCardData("33000010", "Marc Arndt"), IdentityCardInUse(EnvelopeData("44000013"),
Array(GameData("11000014", "Titel 1", "Autor 1", "Verlag 1"), GameData("11000025", "Titel 2", "Autor 1", "Verlag 2"), GameData("11000036", "Titel 2", "Autor 1", "Verlag 2")))))
universalService.barcodeInUse("33000032") should equal(LendingEntityInUse(IdentityCardData("33000032"), NotInUse()))
universalService.barcodeInUse("33000043") should equal(LendingEntityNotExists("33000043"))
universalService.barcodeInUse("33000054") should equal(LendingEntityNotExists("33000054"))
}
}
The "full.xml" dataset has the following content:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<ENVELOPE ID="12" AVAILABLE="true" BARCODE="44000024"/>
<ENVELOPE ID="13" AVAILABLE="true" BARCODE="44000035"/>
<ENVELOPE ID="14" AVAILABLE="false" BARCODE="44000046"/>
<ENVELOPE ID="15" AVAILABLE="true" BARCODE="44000013"/>
<GAME ID="1" AGE="0" AUTHOR="Autor 2" AVAILABLE="true" BARCODE="11000047" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 3" RELEASEYEAR="2016" TITLE="Titel 3"/>
<GAME ID="2" AGE="0" AUTHOR="Autor 1" AVAILABLE="true" BARCODE="11000014" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 1" RELEASEYEAR="2016" TITLE="Titel 1"/>
<GAME ID="3" AGE="0" AUTHOR="Autor 1" AVAILABLE="false" BARCODE="11000070" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 1" RELEASEYEAR="2016" TITLE="Titel 5"/>
<GAME ID="4" AGE="0" AUTHOR="Autor 3" AVAILABLE="true" BARCODE="11000058" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 2" RELEASEYEAR="2016" TITLE="Titel 4"/>
<GAME ID="5" AGE="0" AUTHOR="Autor 1" AVAILABLE="true" BARCODE="11000025" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 2" RELEASEYEAR="2016" TITLE="Titel 2"/>
<GAME ID="6" AGE="0" AUTHOR="Autor 3" AVAILABLE="true" BARCODE="11000069" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 2" RELEASEYEAR="2016" TITLE="Titel 4"/>
<GAME ID="7" AGE="0" AUTHOR="Autor 1" AVAILABLE="true" BARCODE="11000036" NUMBEROFPLAYERS="0" PUBLISHER="Verlag 2" RELEASEYEAR="2016" TITLE="Titel 2"/>
<IDENTITYCARD ID="8" AVAILABLE="true" BARCODE="33000032"/>
<IDENTITYCARD ID="9" AVAILABLE="false" BARCODE="33000043"/>
<IDENTITYCARD ID="10" AVAILABLE="true" BARCODE="33000010"/>
<IDENTITYCARD ID="11" AVAILABLE="true" BARCODE="33000021"/>
<LENDGAME ID="20" LENDTIME="2016-11-05 09:05:00.0" RETURNTIME="2016-11-05 09:20:00.0" GAME_ID="2" LENDIDENTITYCARD_ID="17"/>
<LENDGAME ID="21" LENDTIME="2016-11-05 09:05:00.0" RETURNTIME="2016-11-05 09:25:00.0" GAME_ID="4" LENDIDENTITYCARD_ID="16"/>
<LENDGAME ID="22" LENDTIME="2016-11-05 09:21:00.0" RETURNTIME="2016-11-05 09:25:00.0" GAME_ID="2" LENDIDENTITYCARD_ID="16"/>
<LENDGAME ID="23" LENDTIME="2016-11-05 10:05:00.0" GAME_ID="1" LENDIDENTITYCARD_ID="19"/>
<LENDGAME ID="24" LENDTIME="2016-11-05 10:05:00.0" GAME_ID="2" LENDIDENTITYCARD_ID="18"/>
<LENDGAME ID="25" LENDTIME="2016-11-05 10:05:00.0" GAME_ID="5" LENDIDENTITYCARD_ID="18"/>
<LENDGAME ID="26" LENDTIME="2016-11-05 10:05:00.0" GAME_ID="7" LENDIDENTITYCARD_ID="18"/>
<LENDIDENTITYCARD ID="16" LENDTIME="2016-11-05 09:00:00.0" RETURNTIME="2016-11-05 09:30:00.0" ENVELOPE_ID="13" IDENTITYCARD_ID="8"/>
<LENDIDENTITYCARD ID="17" LENDTIME="2016-11-05 09:00:00.0" RETURNTIME="2016-11-05 09:30:00.0" ENVELOPE_ID="15" IDENTITYCARD_ID="10"/>
<LENDIDENTITYCARD ID="18" LENDTIME="2016-11-05 10:00:00.0" OWNER="Marc Arndt" ENVELOPE_ID="15" IDENTITYCARD_ID="10"/>
<LENDIDENTITYCARD ID="19" LENDTIME="2016-11-05 10:00:00.0" ENVELOPE_ID="12" IDENTITYCARD_ID="11"/>
</dataset>
Now when I try to run my tests I get the following exceptions:
org.jboss.arquillian.persistence.dbunit.exception.DBUnitDataSetHandlingException: Unable to clean database.
at org.jboss.arquillian.persistence.dbunit.cleanup.StrictCleanupStrategyExecutor.cleanupDatabase(StrictCleanupStrategyExecutor.java:60)
at org.jboss.arquillian.persistence.dbunit.DBUnitDataHandler.cleanDatabase(DBUnitDataHandler.java:182)
at org.jboss.arquillian.persistence.dbunit.DBUnitDataHandler.cleanup(DBUnitDataHandler.java:120)
...
Caused by: java.sql.SQLException: IJ031040: Connection is not associated with a managed connection: org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7@102f8e8e
at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:164)
at org.jboss.jca.adapters.jdbc.WrappedConnection.createStatement(WrappedConnection.java:337)
at org.dbunit.database.statement.AbstractBatchStatement.<init>(AbstractBatchStatement.java:50)
at org.dbunit.database.statement.SimpleStatement.<init>(SimpleStatement.java:49)
at org.dbunit.database.statement.PreparedStatementFactory.createBatchStatement(PreparedStatementFactory.java:57)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:85)
at org.jboss.arquillian.persistence.dbunit.cleanup.StrictCleanupStrategyExecutor.cleanupDatabase(StrictCleanupStrategyExecutor.java:56)
... 230 more
Can someone pls tell me how to get my tests running?
Thanks alot
Marc