简体   繁体   中英

How to set up embedded WildFly server on the fly for testing with Maven

I have a question about how to set up an embedded Wildfly 10 server on the fly for integration testing.

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

First Maven download the server on the fly, saves it to the target folder. Later I want to copy a new standalone.xml, start the server, run the integration test and stop the server.

Until now I can't see that I have started the server.

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

Does anybody knows how to set up a Maven for start and stop a embedded Wildfly server, start the test cases and hopefully see some logging?

I recommend to take a look at Arquillian which will take care of the container lifecycle management and runs your tests by deploying them to the container. The nice thing is that this also allows to run tests from within the IDE as the test (runner) itself will manage the container lifecycle.

When controlling the container using the WildFly Maven plug-in, the Maven Failsafe plug-in should be used for this kind of integration tests as it makes sure that the container will be safely shut down after the tests have been executed also in case of failures.

You also could take into these integration tests from Hibernate Search where we use Arquillian to run integration tests. Check out src/test/resources/arquillian.xml where we point to some specific server config files to be used instead of standalone.xml .

This POM also shows how to use the Maven dependency plug-in for downloading WildFly and extracting it before running the tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM