Search code examples
junit5jboss-arquillianthorntail

Howto test with arquillian and Thorntail without @DefaultDeployment


Thorntail doc says to use @DefaultDeployment in order to test a class. I don't want to use this because it generates a deployment which includes a persistence.xml which describes a connection to a production database. In the context of a test the connection fails then the test can't be driven. So I tried to use @Deployment with the ShrinkWrap tool.

@RunWith(Arquillian.class)
//@DefaultDeployment
//@RunAsClient
public class ApplicationScopedForTestTest {
    @Deployment(testable = true)
    public static Archive createDeployment() {

        JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "jdbManagerTEST.jar");
        archive.addPackages(true, "org.avm.business.jdb_manager");
        archive.addAsResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
        archive.addAsResource("META-INF/persistence-test.xml", "project-tests.yml");
        
        // print all included packages
        System.out.println(archive.toString(true));

        return archive;
    }

Then, next lines :

    @Inject
    ApplicationScopedForTest services;
    @Test
    public void print() {
        assertNotNull(services);
    }

No way ; always a java.lang.RuntimeException: Could not inject members ; the root cause is Original exception caused: class java.lang.ClassNotFoundException...

services is never known, CDI fails.

All my classes are included in my archive (system.out tells me so). It's like weld or something is missing to make it work, but what and how?

My POM:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <parent>
  <groupId>org.avm.business</groupId>
  <artifactId>geo3d-services</artifactId>
  <version>1.0</version>
 </parent>

 <modelVersion>4.0.0</modelVersion>
 <artifactId>jdbManager</artifactId>
 <name>Managing JDB</name>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
  <dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <version>42.2.8</version>
  </dependency>
  <dependency>
   <groupId>io.thorntail</groupId>
   <artifactId>datasources</artifactId>
  </dependency>
  <dependency>
   <groupId>io.thorntail</groupId>
   <artifactId>jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>io.thorntail</groupId>
   <artifactId>cdi</artifactId>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.eclipse.microprofile.openapi/microprofile-openapi-api -->
  <dependency>
   <groupId>org.eclipse.microprofile.openapi</groupId>
   <artifactId>microprofile-openapi-api</artifactId>
  </dependency>

  <dependency>
   <groupId>io.thorntail</groupId>
   <artifactId>jaxrs</artifactId>
  </dependency>
  <dependency>
   <groupId>io.thorntail</groupId>
   <artifactId>jaxrs-multipart</artifactId>
  </dependency>

  <!-- TESTS -->
  <dependency>
   <groupId>io.thorntail</groupId>
   <artifactId>arquillian</artifactId>
   <scope>test</scope>
  </dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.arquillian.junit/arquillian-junit-container -->
<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <version>1.5.0.Final</version>
    <scope>test</scope>
</dependency>

 </dependencies>
</project>

and parent POM:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.avm.business</groupId>
 <artifactId>geo3d-services</artifactId>
 <version>1.0</version>
 <packaging>pom</packaging>

 <properties>
  <version.thorntail>2.5.0.Final</version.thorntail>
  <version.arquillian>1.5.0.Final</version.arquillian>
  <version.microprofile>3.2</version.microprofile>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <failOnMissingWebXml>false</failOnMissingWebXml>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <openapi.base.package>org.avm.business.openapi</openapi.base.package>
  <!-- <swagger-core-version>2.0.10</swagger-core-version> -->
 </properties>

 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.eclipse.microprofile</groupId>
    <artifactId>microprofile</artifactId>
    <version>${version.microprofile}</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
   <!-- https://mvnrepository.com/artifact/org.jboss.arquillian/arquillian-bom -->
   <dependency>
    <groupId>io.thorntail</groupId>
    <artifactId>bom</artifactId>
    <version>${version.thorntail}</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>

 <build>
  <finalName>${project.artifactId}</finalName>
  <plugins>
   <plugin>
    <groupId>io.thorntail</groupId>
    <artifactId>thorntail-maven-plugin</artifactId>
    <version>${version.thorntail}</version>

    <executions>
     <execution>
      <goals>
       <goal>package</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
     <execution>
      <phase>install</phase>
      <configuration>
       <target>
        <copy file="target/${project.artifactId}-thorntail.jar"
         todir="../" />
       </target>
      </configuration>
      <goals>
       <goal>run</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
</project>

Output:

mvn test
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< org.avm.business:jdbManager >---------------------
[INFO] Building Managing JDB 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jdbManager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jdbManager ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jdbManager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jdbManager ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jdbManager ---
[INFO] Surefire report directory: /home/lbroque/Projects/GEO3D-services/docker/services/jdbManager/workspace/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.avm.business.jdb_manager.jdb.parser.services.ApplicationScopedForTestTest
jdbManagerTEST.jar:
/org/
/org/avm/
/org/avm/business/
/org/avm/business/jdb_manager/
/org/avm/business/jdb_manager/jdb/
/org/avm/business/jdb_manager/jdb/parser/
/org/avm/business/jdb_manager/jdb/parser/services/
/org/avm/business/jdb_manager/jdb/parser/services/ApplicationScopedForTestTest.class
(...)
/beans.xml
/project-tests.yml
Resolving 0 out of 625 artifacts
Fri Nov 22 16:13:45 CET 2019 INFO [org.wildfly.swarm.bootstrap] (main) Dependencies not bundled; resolving from M2REPO.
2019-11-22 16:13:48,285 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                 Remoting - STABLE          io.thorntail:remoting:2.5.0.Final
2019-11-22 16:13:48,292 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:              Datasources - STABLE          io.thorntail:datasources:2.5.0.Final
2019-11-22 16:13:48,292 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:        CDI Configuration - STABLE          io.thorntail:cdi-config:2.5.0.Final
2019-11-22 16:13:48,292 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:             Transactions - STABLE          io.thorntail:transactions:2.5.0.Final
2019-11-22 16:13:48,292 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                      JMX - STABLE          io.thorntail:jmx:2.5.0.Final
2019-11-22 16:13:48,293 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                      JPA - STABLE          io.thorntail:jpa:2.5.0.Final
2019-11-22 16:13:48,293 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                   JAX-RS - STABLE          io.thorntail:jaxrs:2.5.0.Final
2019-11-22 16:13:48,293 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                  Logging - STABLE          io.thorntail:logging:2.5.0.Final
2019-11-22 16:13:48,293 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:               Infinispan - STABLE          io.thorntail:infinispan:2.5.0.Final
2019-11-22 16:13:48,293 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                      JCA - STABLE          io.thorntail:jca:2.5.0.Final
2019-11-22 16:13:48,293 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                      CDI - STABLE          io.thorntail:cdi:2.5.0.Final
2019-11-22 16:13:48,294 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:               Arquillian - STABLE          io.thorntail:arquillian:2.5.0.Final
2019-11-22 16:13:48,294 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:          Bean Validation - STABLE          io.thorntail:bean-validation:2.5.0.Final
2019-11-22 16:13:48,294 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                 Undertow - STABLE          io.thorntail:undertow:2.5.0.Final
2019-11-22 16:13:48,294 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:                  Elytron - STABLE          io.thorntail:elytron:2.5.0.Final
2019-11-22 16:13:48,294 INFO  [org.wildfly.swarm] (main) THORN0013: Installed fraction:    JAX-RS with Multipart - STABLE          io.thorntail:jaxrs-multipart:2.5.0.Final
2019-11-22 16:13:49,438 INFO  [org.wildfly.swarm.datasources] (main) THORN1003: Auto-detected JDBC driver for postgresql
2019-11-22 16:13:49,499 INFO  [org.wildfly.swarm.jmx] (main) JMX not configured for remote access
2019-11-22 16:13:49,866 INFO  [org.jboss.msc] (main) JBoss MSC version 1.4.5.Final
2019-11-22 16:13:49,872 INFO  [org.jboss.threads] (main) JBoss Threads version 2.3.2.Final
2019-11-22 16:13:49,951 INFO  [org.jboss.as] (MSC service thread 1-2) WFLYSRV0049: Thorntail 2.5.0.Final (WildFly Core 7.0.0.Final) starting
2019-11-22 16:13:49,989 INFO  [org.wildfly.swarm] (MSC service thread 1-2) THORN0019: Install MSC service for command line args: []
2019-11-22 16:13:50,107 INFO  [org.wildfly.swarm.arquillian.daemon.server.Server] (MSC service thread 1-3) Arquillian Daemon server started on localhost:12345
2019-11-22 16:13:50,425 INFO  [org.wildfly.security] (ServerService Thread Pool -- 12) ELY00001: WildFly Elytron version 1.7.0.Final
2019-11-22 16:13:50,747 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 20) WFLYNAM0001: Activating Naming Subsystem
2019-11-22 16:13:50,748 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 23) WFLYCLINF0001: Activating Infinispan subsystem.
2019-11-22 16:13:50,750 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 22) WFLYSEC0002: Activating Security Subsystem
2019-11-22 16:13:50,754 INFO  [org.jboss.as.jaxrs] (ServerService Thread Pool -- 24) WFLYRS0016: RESTEasy version 3.8.1.Final
2019-11-22 16:13:50,757 INFO  [org.jboss.as.security] (MSC service thread 1-7) WFLYSEC0001: Current PicketBox version=5.0.3.Final
2019-11-22 16:13:50,769 WARN  [org.jboss.as.txn] (ServerService Thread Pool -- 34) WFLYTX0013: The node-identifier attribute on the /subsystem=transactions is set to the default value. This is a danger for environments running multiple servers. Please make sure the attribute value is unique.
2019-11-22 16:13:50,782 INFO  [org.jboss.as.naming] (MSC service thread 1-2) WFLYNAM0003: Starting Naming Service
2019-11-22 16:13:50,797 INFO  [org.jboss.as.connector] (MSC service thread 1-3) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.4.11.Final)
2019-11-22 16:13:50,804 INFO  [org.xnio] (ServerService Thread Pool -- 36) XNIO version 3.6.5.Final
2019-11-22 16:13:50,841 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0003: Undertow 2.0.15.Final starting
2019-11-22 16:13:50,858 INFO  [org.xnio.nio] (ServerService Thread Pool -- 36) XNIO NIO Implementation Version 3.6.5.Final
2019-11-22 16:13:50,860 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) WFLYJCA0005: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 42.2)
2019-11-22 16:13:50,863 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0018: Started Driver service with driver-name = postgresql
2019-11-22 16:13:50,879 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 36) WFLYIO001: Worker 'default' has auto-configured to 16 core threads with 128 task threads based on your 8 available processors
2019-11-22 16:13:50,929 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) WFLYJCA0010: Unbound data source [java:jboss/datasources/ExampleDS]
2019-11-22 16:13:50,971 INFO  [org.jboss.remoting] (MSC service thread 1-1) JBoss Remoting version 5.0.8.Final
2019-11-22 16:13:50,993 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0012: Started server default-server.
2019-11-22 16:13:51,040 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTP listener default listening on 0.0.0.0:8080
2019-11-22 16:13:51,083 WARN  [org.jboss.as.remoting] (MSC service thread 1-5) ****** All authentication is ANONYMOUS for org.jboss.as.remoting.RemotingHttpUpgradeService
2019-11-22 16:13:51,149 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
2019-11-22 16:13:51,250 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-3) ISPN000128: Infinispan version: Infinispan 'Infinity Minus ONE +2' 9.4.3.Final
2019-11-22 16:13:51,525 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 37) WFLYCLINF0002: Started default cache from server container
2019-11-22 16:13:51,525 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 38) WFLYCLINF0002: Started passivation cache from web container
2019-11-22 16:13:51,584 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server
2019-11-22 16:13:51,586 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: Thorntail 2.5.0.Final (WildFly Core 7.0.0.Final) started in 1748ms - Started 253 of 382 services (231 services are lazy, passive or on-demand)
2019-11-22 16:13:52,062 INFO  [org.wildfly.swarm.runtime.deployer] (main) deploying jdbManagerTEST.jar
2019-11-22 16:13:52,085 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) WFLYSRV0027: Starting deployment of "jdbManagerTEST.jar" (runtime-name: "jdbManagerTEST.jar")
2019-11-22 16:13:52,583 WARN  [org.jboss.as.dependency.private] (MSC service thread 1-1) WFLYSRV0018: Deployment "deployment.jdbManagerTEST.jar" is using a private module ("org.infinispan") which may be changed or removed in future versions without notice.
2019-11-22 16:13:52,584 WARN  [org.jboss.as.dependency.private] (MSC service thread 1-1) WFLYSRV0018: Deployment "deployment.jdbManagerTEST.jar" is using a private module ("org.infinispan.commons") which may be changed or removed in future versions without notice.
2019-11-22 16:13:52,585 WARN  [org.jboss.as.dependency.private] (MSC service thread 1-1) WFLYSRV0018: Deployment "deployment.jdbManagerTEST.jar" is using a private module ("org.jboss.ironjacamar.jdbcadapters") which may be changed or removed in future versions without notice.
2019-11-22 16:13:52,585 WARN  [org.jboss.as.dependency.private] (MSC service thread 1-1) WFLYSRV0018: Deployment "deployment.jdbManagerTEST.jar" is using a private module ("org.jboss.jts") which may be changed or removed in future versions without notice.
2019-11-22 16:13:52,600 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) WFLYWELD0003: Processing weld deployment jdbManagerTEST.jar
2019-11-22 16:13:52,642 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-1) HV000001: Hibernate Validator 6.0.14.Final
2019-11-22 16:13:52,778 INFO  [org.jboss.weld.Version] (MSC service thread 1-4) WELD-000900: 3.0.5 (Final)
2019-11-22 16:13:53,252 INFO  [org.jboss.as.server] (main) WFLYSRV0010: Deployed "jdbManagerTEST.jar" (runtime-name : "jdbManagerTEST.jar")
2019-11-22 16:13:53,258 INFO  [org.wildfly.swarm] (main) THORN99999: Thorntail is Ready

Sorry for the length of the post; I tried to put everything useful.


Solution

  • The problem is on this line:

    archive.addAsResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
    

    You're adding a beans.xml file to the root of the JAR, as can also be seen in the log you print:

    /beans.xml
    

    This is wrong. In case of a JAR, beans.xml should be present in META-INF. See this answer https://stackoverflow.com/a/9250108/742081 for where beans.xml should be located. To put it into META-INF in a JAR, you should use archive.addAsManifestResource(...).