0

I try to implement Arquillian for unit tests into my maven project.

This is the pom.xml of my project:

 <dependencies>

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

    <dependency>
        <groupId>org.jboss.ejb3</groupId>
        <artifactId>jboss-ejb3-ext-api</artifactId>
        <version>2.1.0</version>
    </dependency>

    <dependency>
        <groupId>op.coma</groupId>
        <artifactId>op-interfaces-coma</artifactId> <!-- where i have all my entities -->
        <version>1.0.0</version>
    </dependency>
</dependencies>

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

<build>
    <sourceDirectory>ejbModule</sourceDirectory>
    <testSourceDirectory>ejbModule/src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <directory>ejbModule</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>Cp1252</encoding>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>${maven-ejb-plugin.version}</version>
            <configuration>
                <ejbVersion>${ejb.version}</ejbVersion>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <artifactSet>
                    <includes>
                        <include>op.coma</include>
                    </includes>
                </artifactSet>
                <finalName>${artifactId}-${project.version}</finalName>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <configuration>
                <filename>${artifactId}-${project.version}.jar</filename>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skip>false</skip>
                <childDelegation>true</childDelegation>
                <useSystemClassLoader>true</useSystemClassLoader>
                <argLine>-Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
                <suiteXmlFiles>
                    <suiteXmlFile>ejbModule/src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

    </plugins>
</build>
<profiles>

    <profile>
        <id>arquillian-glassfish-embedded</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian.container</groupId>
                <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                <version>1.0.0.CR3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-all</artifactId>
                <version>3.1.2</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>${h2.version}</version>
                <scope>test</scope>
            </dependency>

        </dependencies>
        <build>
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                </testResource>
                <testResource>
                    <directory>src/test/resources-glassfish-embedded</directory>
                </testResource>
            </testResources>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12</version>
                    <configuration>
                        <systemPropertyVariables>
                            <java.util.logging.config.file>
                                ${project.build.testOutputDirectory}/logging.properties
                            </java.util.logging.config.file>
                            <derby.stream.error.file>
                                ${project.build.directory}/derby.log
                            </derby.stream.error.file>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

</profiles>

This is my test Class:

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
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;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class PersonFactoryTest extends Arquillian {    
@Deployment
public static JavaArchive  createDeployment() {
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
            .addPackage(PersonFactory.class.getPackage())
            .addAsManifestResource("test-persistence.xml", "persistence.xml")
            .addAsManifestResource("jbossas-ds.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    return jar;
}
@BeforeTest
private void insertData() {
    Person person = new Person();;
    person.setName("TestName");
    person.setLastName("TestLastName");
    person.setClientId(10);
}
@Test
public void findListByIDClientTest() throws Exception {
    int i=12;
    Assert.assertTrue(i==14);
}}    

This is the glassfish-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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/arquillian"/>
<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:file:target/databases/h2/db"/>
</jdbc-connection-pool>
</resources>

For the test-persistence.xml and persistence.xml:

<?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="test">
   <jta-data-source>jdbc/arquillian</jta-data-source>
     <properties>
     <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
     <property name="eclipselink.logging.level.sql" value="FINE"/>
     <property name="eclipselink.logging.parameters" value="true"/>
     </properties>
     </persistence-unit>
</persistence>

When i make a maven clean and install it gives me the following errors:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing javac, but could not parse the error:
An annotation processor threw an uncaught exception.
Consult the following stack trace for details.
java.lang.IllegalArgumentException: The argument does not represent an annotation type: Deployment
at com.sun.tools.javac.processing.JavacRoundEnvironment.getElementsAnnotatedWith(JavacRoundEnvironment.java:119)
at org.hibernate.validator.ap.ConstraintValidationProcessor.process(ConstraintValidationProcessor.java:107)
...
3
  • Please add your imports.
    – John Ament
    Apr 24, 2015 at 9:16
  • @John Ament , it's done Apr 24, 2015 at 9:36
  • Why have you changed the source and test source folder instead of using the defaults? I would suggest to use the defaults.
    – khmarbaise
    Apr 24, 2015 at 17:27

1 Answer 1

0

Above PersonFactoryTest you should use the @RunWith(Arquillian.class)

I have recently written some Arquillian tests for a HTTP Resource Apaptor see https://github.com/ozoli/http-jca-adaptor Hope this helps!

1
  • i don't use Junit to use RunWith(Arquillian.class) but as you see i use testng May 4, 2015 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.