Arquillian – testy integracyjne


Arquillian – testy integracyjne

Arquillian to framework który służy do pisania testów integracyjnych. Testy wykonywane są wewnątrz realnego środowiska uruchomieniowego, którego cykl życia w pełni zarządzany jest przez Arquilliana:

W pliku pom.xml projektu dodajemy zależności:

<profiles>
  <profile>
	<id>arquillian-jbossas-managed</id>
	<dependencies>
		<dependency>
		   <groupId>org.jboss.arquillian.junit</groupId>
	           <artifactId>arquillian-junit-container</artifactId>
		   <scope>test</scope>
		</dependency>

		<dependency>
		    <groupId>org.jboss.spec</groupId>
		    <artifactId>jboss-javaee-6.0</artifactId>
		    <version>1.0.0.Final</version>
		    <type>pom</type>
		    <scope>provided</scope>
		</dependency>

		<dependency>
		    <groupId>org.wildfly.arquillian</groupId>
		    <artifactId>wildfly-arquillian-container-managed</artifactId>
		    <version>2.1.1.Final</version>
		    <scope>test</scope>
		</dependency>

		<dependency>
	            <groupId>org.jboss.arquillian.protocol</groupId>
		    <artifactId>arquillian-protocol-servlet</artifactId>
		    <scope>test</scope>
		</dependency>

		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-core</artifactId>
		    <version>5.3.9.Final</version>
		    <scope>provided</scope>
		</dependency>
	</dependencies>
  </profile>
</profiles>

oraz zarządzanie zależnościami:

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

Tworzymy przykładowy test integracyjny:

@RunWith(Arquillian.class)
public class TestHalloEJB {

    @Deployment
    public static JavaArchive createDeployment(){
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "halloEjb.jar");
        System.out.println(createPropertyJar(jar).toString(true));
        return jar;
    }
    private static JavaArchive createPropertyJar(JavaArchive javaArchive) {
        javaArchive.addClass(HalloEjbLocal.class).addPackages(true,"pl.edusession");
        javaArchive.addPackages(true, "javax.persistence");
        javaArchive.addAsResource("META-INF/hibernate/persistence.xml", 
                                  "META-INF/persistence.xml");
        javaArchive.addAsManifestResource(new StringAsset("Dependencies: 
                                          com.h2database.h2"), "MANIFEST.MF");
        javaArchive.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        return javaArchive;
    }
}

Plik arquillian.xml:

<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="jbossas-7-managed" default="true">
        <configuration>
            <property name="jbossHome">C:\Users\EMAWARY\Downloads\wildfly-16.0.0.Final\wildfly-16.0.0.Final</property>
        </configuration>
    </container>
</arquillian>

Jeśli chcemy użyć jako providera JPA – EclipseLink należy do katalogu:

C:\Users\EMAWARY\Downloads\wildfly-16.0.0.Final\wildfly-16.0.0.Final\modules\system\layers\base\org\eclipse\persistence\main

dodać plik jar: eclipselink-2.6.0 oraz jipijapa-eclipselink-16.0.0.Final. Następnie należy w tym samym katalogu zmodyfikować plik module.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2011, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->
<!-- Represents the EclipseLink module  -->
<module name="org.eclipse.persistence" xmlns="urn:jboss:module:1.5">
    <properties>
        <property name="jboss.api" value="public"/>
    </properties>

    <resources>
        <resource-root path="jipijapa-eclipselink-16.0.0.Final.jar"/>
		  <resource-root path="eclipselink-2.6.0.jar">
           <filter>
              <exclude path="javax/**" />
           </filter>
        </resource-root>
    </resources>

   <dependencies>
        <module name="asm.asm"/>
        <module name="javax.api"/>
        <module name="javax.annotation.api"/>
        <module name="javax.enterprise.api"/>
        <module name="javax.persistence.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.validation.api"/>
        <module name="javax.xml.bind.api"/>
        <module name="javax.ws.rs.api"/>
        <module name="org.antlr"/>
        <module name="org.apache.commons.collections"/>
        <module name="org.dom4j"/>
        <module name="org.jboss.as.jpa.spi"/>
        <module name="org.jboss.logging"/>
        <module name="org.jboss.vfs"/>
    </dependencies>
</module>

Po powyższych zmianach w aplikacji w pliku persistence.xml można dodać:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>


Leave a comment

Your email address will not be published.


*