java - 如何动态设置嵌入式 WildFly 服务器以使用 Maven 进行测试

标签 java maven jboss integration-testing wildfly-10

我有一个关于如何动态设置嵌入式 Wildfly 10 服务器以进行集成测试的问题。

<!-- Loading Wildfly 10 on the fly and copy it into the target folder. -->
<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>10.0.0.Final</version>
                        <type>zip</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>target</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.wildfly.plugins</groupId>
    <artifactId>wildfly-maven-plugin</artifactId>
    <version>1.1.0.Alpha1</version>
    <configuration>
        <jbossHome>target/wildfly-10.0.0.Final</jbossHome>
        <hostname>127.0.0.1</hostname>
        <!--  <port>9990</port> -->
        <filename>${project.build.finalName}.war</filename>
        <java-opts>
            <java-opt>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005</java-opt>
        </java-opts>
        <commands>
            <command>/subsystem=logging/file-handler=debug:add(level=DEBUG,autoflush=true,file={"relative-to"=>"jboss.server.log.dir",
                "path"=>"debug.log"})</command>
            <command>/subsystem=logging/logger=org.jboss.as:add(level=DEBUG,handlers=[debug])</command>
        </commands>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>deploy</goal>
            </goals>
        </execution>
        <execution>
            <id>start-wildfly</id>
            <phase>test-compile</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
        <execution>
            <id>shutdown-wildfly</id>
            <phase>test</phase>
            <goals>
                <goal>shutdown</goal>
            </goals>
        </execution>
    </executions>
</plugin>

首先Maven动态下载服务器,保存到目标文件夹。稍后我想复制一个新的 standalone.xml,启动服务器,运行集成测试并停止服务器。

直到现在我还看不到我已经启动了服务器。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.FunctionalTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.071  sec <<< FAILURE! - in com.example.FunctionalTest
basicPingTest(com.example.FunctionalTest)  Time elapsed: 0.07 sec  <<< ERROR!
java.net.ConnectException: Connection refused
at com.example.FunctionalTest.basicPingTest(FunctionalTest.java:39)

Results :

Tests in error: 
 FunctionalTest.basicPingTest:39 » Connect Connection refused

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

有人知道如何设置 Maven 来启动和停止嵌入式 Wildfly 服务器、启动测试用例并希望看到一些日志记录吗?

最佳答案

我建议看一下 Arquillian它将负责容器生命周期管理并通过将它们部署到容器来运行您的测试。好的是,这还允许从 IDE 中运行测试,因为测试(运行器)本身将​​管理容器生命周期。

当使用 WildFly Maven 插件控制容器时,Maven Failsafe 插件应该用于此类集成测试,因为它确保在执行测试后容器将安全关闭失败案例。

您还可以考虑 these integration tests来自 Hibernate Search,我们在其中使用 Arquillian 运行集成测试。查看 src/test/resources/arquillian.xml,我们在其中指向一些要使用的特定服务器配置文件,而不是 standalone.xml

此 POM 还展示了如何使用 Maven 依赖插件下载 WildFly 并在运行测试之前将其解压缩。

关于java - 如何动态设置嵌入式 WildFly 服务器以使用 Maven 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380610/

相关文章:

java - 2960x1440 拉伸(stretch) Android 相机预览

java - 没有wsdl的apache cxf客户端

java - OSGI 中的 Google 客户端 API

intellij-idea - intellij JBoss standalone.xml 文件更改被覆盖

logging - 如何在我的应用程序中使用 jboss-log4j.xml

java - 在jboss独立模式下设置数据源

java - Jenkins:将 war 文件部署到 Wildfly 失败

Java/Android - GettingClassCast 异常将基类转换为子类以调用子类方法

java - Maven + TestNG 打印@DataProvider的参数

tomcat - 如何使用 Tomcat 在 Maven 构建阶段运行 selenium 测试