javajboss-arquillianjava-ee-8

Unable to execute Arquillian test


I am trying to use Arquillian to conduct integration tests of my web app. I am very new to Arquillian and have spent considerable time to figure out how to use Arquillian. There are number of tutorials that are old and I am not sure if there are conflicts in the versions that have been used. Based on the tutorials and the examples I found, I put together the pom.xml file (shown below), arquillian.xml (shown below). The basic example that I am trying to test within Eclipse (March 2019 Eclipse version) is not executed and I dont see any errors on the console as well which puzzles me. I also read some posts which says I need to add a classpath in my eclipse but unsure if that is necessary and could not find anywhere how to do so. I am using Wildfly 16. I would very much appreciate if you could point me to where the problem is?

The test is simple I am injecting the service object and trying to persist the Portfolio Object. To run the test from within Eclipse I am right clicking on the Test file and Debug As Junit Test (running Junit 5) and I dont see the server start up and the portfolioService is always null. I also have a print for the Deployment Archive but I dont see anything in the console which makes me think that it is not getting executed.

pom.xml

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

<groupId>com.example</groupId>
<artifactId>Samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <version.arquillian>1.6.0.Final</version.arquillian>
    <version.wildfly>16.0.0.Final</version.wildfly>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>${version.arquillian}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <finalName>${project.artifactId}</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.1</version>
            </plugin>

        </plugins>
    </pluginManagement>
</build>

<profiles>
    <!-- Arquillian WildFly managed profile -->
    <profile>
        <id>arq-wildfly-managed</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-arquillian-container-managed</artifactId>
                <version>8.2.1.Final</version>
                <exclusions>
                    <exclusion>
                        <groupId>sun.jdk</groupId>
                        <artifactId>jconsole</artifactId>
                    </exclusion>
                </exclusions>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                </testResource>
            </testResources>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.16</version>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <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>${version.wildfly}</version>
                                            <type>zip</type>
                                            <overWrite>false</overWrite>
                                            <outputDirectory>${project.build.directory}</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.2.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-math3</artifactId>
        <version>3.6.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.26</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-runner -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>1.6.2</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.arquillian.protocol</groupId>
        <artifactId>arquillian-protocol-servlet</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

arquillian.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- Uncomment to have test archives exported to the file system for inspection -->
    <!-- <engine> -->
    <!-- <property name="deploymentExportPath">target/</property> -->
    <!-- </engine> -->

    <!-- Force the use of the Servlet 3.0 protocol with all containers, as it 
        is the most mature -->
    <defaultProtocol type="Servlet 3.0" />

    <!-- Example configuration for a remote WildFly instance -->
    <container qualifier="arq-wildfly-managed" default="true">
        <!-- By default, arquillian will use the JBOSS_HOME environment variable. 
            Alternatively, the configuration below can be uncommented. -->
        <configuration>
            <property name="jbossHome">${jboss.home}</property>
        </configuration>
    </container>
</arquillian>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="mysql" transaction-type="JTA">
        <jta-data-source>java:/MySqlDS</jta-data-source>
        <!-- <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> -->
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQL57Dialect" />
            <property
                name="javax.persistence.schema-generation.database.action"
                value="drop-and-create" />
            <property name="hibernate.format_sql" value="false" />
        </properties>
    </persistence-unit>

    <persistence-unit name="Samples"
        transaction-type="JTA">
        <jta-data-source>java:/SampleDS</jta-data-source>
        <!-- <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> -->
        <properties>
            <!-- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" 
                /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testdb" 
                /> <property name="javax.persistence.jdbc.user" value="root" /> <property 
                name="javax.persistence.jdbc.password" value="ravindra123" /> -->
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQL57Dialect" />
            <property
                name="javax.persistence.schema-generation.database.action"
                value="update" />
            <property name="hibernate.format_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

Test Example

package com.example.view;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;

import com.example.model.Portfolio;
import com.example.service.PortfolioService;

@RunWith(Arquillian.class)
class PortfolioBeanTest {

    @Deployment
    public static WebArchive createDeployment() {
        WebArchive wArchive =  ShrinkWrap.create(WebArchive.class, "test.war")
                .addClasses(Portfolio.class, PortfolioService.class)
                .addAsResource("META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(wArchive.toString(true));
        return wArchive;
    }

    @Inject
    PortfolioService portfolioService;

    @Test
    void testCreatePortfolio() {
        Portfolio p = new Portfolio();
        String name = "Test_Portfolio";
        p.setPortfolioName(name);
        boolean created = portfolioService.create(p);

        List<Portfolio> portfolioList = portfolioService.getAllPortfolios();
        assertEquals(1, portfolioList.size());

        String portfolioName = portfolioList.get(0).getPortfolioName();
        assertEquals(name, portfolioName);
    }

}

Service.java

package com.example.service;

import java.lang.invoke.MethodHandles;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.model.Portfolio;

@Stateless
public class PortfolioService {
    @PersistenceContext(unitName = "mysql")
    private EntityManager em;

    private static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass().getSimpleName());

    public boolean create(Portfolio portfolio) {
        boolean created = true;
        String name = portfolio.getPortfolioName();
        portfolio.setPortfolioName(name);
        em.persist(portfolio);
        logger.debug("Created portfolio {}", portfolio);

        return created;
    }

    public List<String> getAllPortfolioNames() {
        TypedQuery<String> query = em.createQuery("SELECT p.portfolioName FROM Portfolio p", String.class);
        List<String> results = query.getResultList();
        return results;
    }


    public Portfolio find(Long id) {
        return em.find(Portfolio.class, id);
    }


}

Solution

  • I review your pom.xml and I think that

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.2.Final</version>
    </dependency>
    

    will cash with the wildfly-arquillian-container-managed. To avoid this, you can add the scope as provided.

    However I suggest a more simpler approach to your pom.xml. Just start with a maven project archetype, for example I use wildfly-jakartaee-webapp-archetype that is related to a Wildfly 21 (jakarta ee 8).

    For instance, to the wildfly-jakartaee-webapp-archetype generated pom.xml you can add only:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-math3</artifactId>
        <version>3.6.1</version>
    </dependency>
    

    And relay on junit 4.x library and the java logging API.

    To run you code you have to activate the arq-managed maven profile, install a local wildfly and define the JBOSS_HOME variable.