无法执行 Arquillian 测试

Unable to execute Arquillian test

我正在尝试使用 Arquillian 对我的网络应用程序进行集成测试。我是 Arquillian 的新手,并且花了相当多的时间来弄清楚如何使用 Arquillian。有很多旧的教程,我不确定使用的版本是否存在冲突。根据我找到的教程和示例,我将 pom.xml 文件(如下所示)、arquillian.xml(如下所示)放在一起。我试图在 Eclipse(2019 年 3 月 Eclipse 版本)中测试的基本示例没有执行,我也没有在控制台上看到任何错误,这让我很困惑。我还阅读了一些帖子,这些帖子说我需要在我的日食中添加一个类路径,但不确定是否有必要并且无法在任何地方找到如何这样做。我正在使用 Wildfly 16。如果您能指出问题出在哪里,我将不胜感激?

测试很简单,我正在注入服务对象并尝试保留投资组合对象。为了从 Eclipse 中进行 运行 测试,我右键单击测试文件并调试为 Junit 测试 (运行ning Junit 5),但我没有看到服务器启动并且 portfolioService 始终为空。我也有部署存档的打印件,但我在控制台中没有看到任何让我认为它没有被执行的东西。

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>

测试示例

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);
    }


}

我查看了您的 pom.xml 我认为

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

将通过 wildfly-arquillian-container-managed 兑现。为避免这种情况,您可以添加提供的范围。

不过,我建议您使用更简单的方法 pom.xml。只需从 Maven 项目原型开始,例如我使用与 Wildfly 21 (jakarta ee 8) 相关的 wildfly-jakartaee-webapp-archetype。

例如,对于生成的 wildfly-jakartaee-webapp-archetype pom.xml,您只能添加:

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

并中继 junit 4.x 库和 java 日志记录 API。

为了 运行 你的代码必须激活 arq-managed maven 配置文件,安装本地 wildfly 并定义 JBOSS_HOME 变量。