我正在尝试使用arquillian和wildfly-14.0.1.Final作为托管容器进行集成测试 . 一切正常,容器启动并部署存档 . 不幸的是,Test-Class中的注入不起作用 . 测试类中的每个注入点都为空 .

我已经尝试在单个会话bean( @Singleton @Startup )上的容器中注入,一切正常 . 所以我认为像beans.xml这样的常见嫌疑人不是问题所在 . 问题从通过arquillian运行的测试类开始 .

我应该明确指出的一件事是,我使用JUnit 5运行所有测试 . 对于Arquillian部分,我使用junit-vintage-engine .

这里是我的设置的简要概述,以及我在一个最小的例子中尝试做的事情 . 我将为测试留下一些不必要的依赖项,以便我们可以识别类路径上的一些错误 .

我希望有人可以把我推向正确的方向因为我不知道当下有什么不对 .

谢谢@ll

pom.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <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>
        <arquillian-version>1.4.1.Final</arquillian-version>
        <junit-vintage-version>4.12.0</junit-vintage-version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>26.0-jre</version>
            </dependency>

            <!-- jee -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.6</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.7</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.7</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.7</version>
            </dependency>

            <!-- test -->

            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.2.0</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>${junit-vintage-version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.2.0</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>${mockito-version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-junit-jupiter</artifactId>
                <version>${mockito-version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-reflect</artifactId>
                <version>1.7.4</version>
                <scope>test</scope>
            </dependency>

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

            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-bom</artifactId>
                <version>3.1.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-depchain</artifactId>
                <type>pom</type>
                <scope>test</scope>
            </dependency>

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

            <dependency>
                <groupId>org.jboss.arquillian.junit</groupId>
                <artifactId>arquillian-junit-container</artifactId>
                <version>${arquillian-version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.jboss.arquillian.protocol</groupId>
                <artifactId>arquillian-protocol-servlet</artifactId>
                <version>${arquillian-version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-common</artifactId>
                <version>2.27</version>
                <scope>test</scope>
            </dependency>


            <!-- end testing -->

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.sonarsource.scanner.maven</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                    <!-- Version 3.3 has the error 'Unable to execute SonarQube: Fail to get bootstrap index from server'
                         with SonarQube 5.6.7 -->
                    <version>3.2</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.22.0</version>
                    <configuration>
                        <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.2</version>
                    <executions>
                        <execution>
                            <id>default-prepare-agent</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-report</id>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
         <profile>
            <id>enable-jacoco</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <argLine>@{argLine} -Dfile.encoding=${project.build.sourceEncoding}</argLine>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>arq-wildfly-managed</id>
            <dependencies>
                <dependency>
                    <groupId>org.wildfly</groupId>
                    <artifactId>wildfly-arquillian-container-managed</artifactId>
                    <version>8.2.1.Final</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <!-- Fork every test because it will launch a separate AS instance -->
                            <forkMode>always</forkMode>
                            <systemPropertyVariables>
                                <!--<arquillian.launch>wildfly-managed</arquillian.launch>-->
                                <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                                <persistenceUnit>UNIT_NAME</persistenceUnit>
                                <dataSource>java:jboss/ds/testingDS</dataSource>
                                <jboss.home>${user.dir}\project\test\wildfly-14.0.1.Final\</jboss.home>
                            </systemPropertyVariables>
                            <includes>
                                <include>%regex[.*DatabaseBaseTest.*]</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

最小测试类:

import javax.inject.Inject;

/**
 * @author David Witte (msg DAVID GmbH)
 * @since 17.08.2018 16:51
 */
@RunWith (Arquillian.class)
public class DatabaseBaseTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseBaseTest.class);

    @Deployment (testable = false)
    public static WebArchive createDeployment() {

            return ShrinkWrap
                    .create(WebArchive.class)
                    .addAsManifestResource("beans.xml", "beans.xml")
                    .addAsWebInfResource("web.xml", "web.xml    ")
                    .addClass(SimpleBeanTest.class)
                    .addClass(SimpleSingleton.class)
                    .addClass(TestApplication.class)
                    .addClass(TestRessource.class);
    }

    @Inject
    public SimpleBeanTest simpleBeanTest;

    //This test runs fine!
    @Test 
    @RunAsClient
    public void testRessource(@ArquillianResource URL baseURL) {

       CloseableHttpClient httpclient = HttpClients.createDefault();
       HttpGet httpGet = new HttpGet(baseURL.toString() + "test/ressource");

       LOGGER.info("Ressource URI is: " + baseURL.toString() + "test/ressource");
       try {
           CloseableHttpResponse response = httpclient.execute(httpGet);
           //Works! Test does not fail!
           Assert.assertEquals(200, response.getStatusLine().getStatusCode());
       }
       catch (IOException e) {
           LOGGER.error("could not execute request", e);
       }

   }

    //This test fails with NPE!
    @Test
    public void firstTestWithArquillian() throws Exception {
        //Here NPE!
        simpleBeanTest.doTestMessage();
        Assert.assertNotNull(simpleBeanTest);
    }
}

SimpleBeanTest.java:

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

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

/**
 * TODO: Describe this file!
 *
 * @author David Witte (msg DAVID GmbH)
 * @since 01.12.2018 17:06
 */
@Named
@RequestScoped
public class SimpleBeanTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBeanTest.class);

    @PostConstruct
    public void init() {
        LOGGER.info("Here I am! I'm running in the Container!");
    }

    public void doTestMessage() {
        LOGGER.info("Hello world!");
    }

    @PreDestroy
    public void destruct() {
        LOGGER.info("Bye, I'm shutting down now!");
    }

}

SimpleSingleton.java:

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

/**
 * TODO: Describe this file!
 *
 * @author David Witte (msg DAVID GmbH)
 * @since 02.12.2018 04:26
 */
@Singleton
@Startup
public class SimpleSingleton {

    //Injection works fine here!
    @Inject
    SimpleBeanTest simpleBeanTest;

    @PostConstruct
    public void init() {
        //No problem after bean creation injection works!
        simpleBeanTest.doTestMessage();
    }

}

TestRessource.java:

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

/**
 * TODO: Describe this file!
 *
 * @author David Witte (msg DAVID GmbH)
 * @since 02.12.2018 16:29
 */
@Path ("ressource")
public class TestRessource {

    //Works fine here!
    @Inject
    SimpleBeanTest simpleBeanTest;

    @GET
    public Response doGet() {

        simpleBeanTest.doTestMessage();
        if (null != simpleBeanTest) {
            return Response.ok().build();
        }
        else {
            return Response.serverError().build();
        }
    }
}