Links and sources
TomEE and Arquillianpersistence extension
How To
POM files and project setup
To understand the following notes for configuring arquillian, some explanations of the project structure should be made. The application code is parted in two subprojects. The web-subproject contains all html and frontend stuff whereas the persistence-subproject contains ejb-services for the database-access and some business-logic. All projects are maven projects and has a pom-file to manage the build-process and dependencies.mainproject
> web-subproject
> persistence-subproject
For running the application, a war-archive is created and deployed on TomEE application server.
Configuration of the pom.xml in the main-project
In the pom.xml of the mainproject, the aquillian dependencies have to be added. This can be done by taking each reference seperately or by using the bom:<dependency> <groupid>org.jboss.arquillian</groupid> <artifactid>arquillian-bom</artifactid> <version>1.1.2.Final</version> <scope>import</scope> <type>pom</type> </dependency>It is also essential to have a container which implements CDI / EJB. For this example, the TomEE embedded container is used, because the TomEE server was chosen to run the JEE appication.
<dependency> <groupid>org.apache.openejb</groupid> <artifactid>arquillian-tomee-embedded</artifactid> <version>1.0.0</version> </dependency>And, of course, JUnit (or TestNG) is nessecary for writing tests.
<dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency>OPTIONAL: The surfire plugin is used with the following system properties to execute the test.
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version>2.16</version> </plugin>I took another extension to have only one deployment for several JUnit tests. A deployment is a creation of a test-jar which can be executed in the maven-build-process.
<dependency> <groupId>org.eu.ingwar.tools</groupId> <artifactId>arquillian-suite-extension</artifactId> <version>1.0.6</version> </plugin>
Configuration of the pom.xml in a sub-project
If you are using sub-projects, add the following dependencies for junit and aquillian in the pom of the sub-project.<dependency> <groupId>org.apache.openejb</groupId> <artifactId>openejb-core</artifactId> <scope>provided</scope> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <scope>test</scope> </dependency> <dependency> <groupid>org.eu.ingwar.tools</groupid> <artifactid>arquillian-suite-extension</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.apache.openejb</groupid> <artifactid>arquillian-tomee-embedded</artifactid> <scope>test</scope> </dependency>
!IMPORTANT!
- the arquillian-tomee-embedded has to be the last entry of the dependencies.
Add a persistence-configuration for tests
under src/test/resources/META-INF, you can define a JPA-configuration (persistence.xml) especially for tests. For avoiding to start an external database before running the tests, h2 can be used as embedded database. Be careful with property "hibernate.hbm2ddl.auto". If the value is configured as "create", all tables will be droped and created again before test execution.<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>de.starwit.lirejarp.entity.CategoryEntity</class>
<class>de.starwit.lirejarp.entity.NewsEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.url" value="jdbc:h2:~/test"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
Create an arquillian test-deployment
At first, you should configure the Deployment. If you are using the arquillian-suite-extension, you can create a class for it, e.g. Deployment.java.package de.starwit.lirejarp.ejb;
import org.eu.ingwar.tools.arquillian.extension.suite.annotations.ArquillianSuiteDeployment;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
/**
* For description https://github.com/ingwarsw/arquillian-suite-extension
*
* @author Anett
*
*/
@ArquillianSuiteDeployment
public class Deployments {
@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create(JavaArchive.class, "test.jar").addPackages(true, "de/starwit/lirejarp")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml");
}
}
In this case, I create an jar-file named "test.jar" which contains all tests of package "de.starwit.lirejarp" in the maven-test-folder. For working with cdi, you need to add a beans.xml to your meta-inf directory. .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") automatically creates one. At least, the persistence-configuration from the test/resources folder should be added.
Keine Kommentare :
Kommentar veröffentlichen