【问题标题】:Glassfish container via Cargo isn't stopped by maven when integration tests fail当集成测试失败时,通过 Cargo 的 Glassfish 容器不会被 maven 停止
【发布时间】:2013-01-12 15:00:10
【问题描述】:

我已将 Cargo 设置为在 maven 配置文件中的预集成测试阶段启动 glassfish 实例。然后我的测试在集成测试阶段运行,最后,cargo 在集成测试后阶段关闭 tomcat 实例。

当所有测试都通过时,这很好用,但是如果任何测试失败,maven 构建就会失败,并且似乎永远不会达到 post-integration-test 阶段,这会使 glassfish 实例运行(我无法停止它不杀死进程)。

我做错了吗?有没有办法确保 cargo 关闭我的 glassfish 实例,即使集成测试阶段失败?

我的 Maven 个人资料:

<profile>
    <!-- run integration tests against the app deployed to a container -->
    <id>integration</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <!-- override the exclusion and include integration tests -->
                            <excludes>
                                <exclude>none</exclude>
                            </excludes>
                            <includes>
                                <include>***IntegrationTest.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>${cargo.plugin.version}</version>
                <executions>
                    <execution>
                        <id>start-server</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-server</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <container>
                        <containerId>glassfish3x</containerId>
                        <artifactInstaller>
                            <groupId>org.glassfish.main.distributions</groupId>
                            <artifactId>glassfish</artifactId>
                            <version>${glassfish.version}</version>
                        </artifactInstaller>
                    </container>
                    <configuration>
                        <properties>
                            <cargo.datasource.datasource.mysql>
                                cargo.datasource.jndi=jdbc/TrackerPool|
                                cargo.datasource.driver=com.mysql.jdbc.Driver|
                                cargo.datasource.url=jdbc:mysql://localhost/[database]|
                                cargo.datasource.transactionsupport=LOCAL_TRANSACTION|
                                cargo.datasource.username=[username]|
                                cargo.datasource.password=[password]
                            </cargo.datasource.datasource.mysql>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

【问题讨论】:

    标签: java maven glassfish integration-testing cargo


    【解决方案1】:

    问题仅仅在于 maven-surefire-plugin 的错误使用,该插件旨在与单元测试相关,但用于集成测试。为此,maven-failsave-plugin 的存在将解决您的问题。

    使用 maven-failsave-plugin 可以让您摆脱对 include rule for integration tests 的定义。 Maven 中用于集成测试的常用命名约定如下:

     IT*.java
     *IT.java
     *ITCase.java
    

    所以我建议相应地命名您的集成测试,这样您就不需要任何类型的排除/包含规则,既不需要用于 maven-surefire-plugin(单元测试)也不需要用于 maven-failsafe-plugin(集成测试)。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.13</version>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    仅当您希望在集成测试失败的情况下使构建失败时才需要验证目标。你必须像这样调用 maven:

    mvn -Pprofile clean verify 
    

    【讨论】:

    • 哦,太棒了 - 这大大简化了事情!感谢您的帮助。
    猜你喜欢
    • 2011-09-22
    • 2011-03-09
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多