当jetty配置为在集成前/集后测试时开始/结束时,如何运行junit测试?

时间:2014-04-10 13:07:14

标签: maven junit maven-jetty-plugin

我想运行junit测试,而jetty正在运行,如何正确配置? 现在似乎测试在jetty启动之前运行,因此他们无法建立连接并且项目构建失败。

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.7.1</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.10.v20130312</version>
            <configuration>
                <connectors>
                    <connector implementation="org.eclipse.jetty.server.bio.SocketConnector">
                        <port>9090</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                </connectors>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
                <jvmArgs>-Xms1024m -Xmx2048m</jvmArgs>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit4</artifactId>
                    <version>2.16</version>
                </dependency>
            </dependencies>
        </plugin>

请帮帮我!如何确保junit测试(在maven构建期间)在集成阶段运行?

1 个答案:

答案 0 :(得分:0)

Maven test阶段用于运行&#39; Unit&#39;测试,即小型测试,检查可能的最小功能或单元&#39;正在按预期运作。

这些是由Maven Surefire插件运行的。你可以在这里找到更多相关信息: http://maven.apache.org/surefire/maven-surefire-plugin/

您拥有的是集成测试。也就是说,它们是更广泛的测试,可以检查代码的不同部分是否集成在一起。这是由Maven Failsafe插件运行的,您可以在此处找到更多详细信息: http://maven.apache.org/surefire/maven-failsafe-plugin/

您需要做的是将集成测试确定为对Failsafe插件的集成测试。默认情况下,它会选择名为**/IT*.java**/*IT.java**/*ITCase.java的类。默认情况下,插件之间的命名约定是互斥的,因此您的测试只能在一个或另一个中运行。

如果您只有集成测试,那么您可以将Surefire配置为在integration-test阶段运行。关于这样做的说明: http://docs.codehaus.org/pages/viewpage.action?pageId=62120