【问题标题】:Jenkins: After build process boot Jetty and start a test against itJenkins:在构建过程启动 Jetty 并开始对其进行测试
【发布时间】:2018-03-12 22:38:28
【问题描述】:

如何以最佳方式设计 Jenkins 作业,其中 Java 项目由 Maven 构建,然后启动 Jetty,以便最终 JUnit 测试类可以对其进行测试?

步骤:

  1. 通过 Maven 构建项目
  2. 启动(嵌入式)码头
  3. 针对 Jetty 中的 RESTful Web 服务执行测试

我目前的问题是,当启动 Jetty 时,由于启动的 Jetty 挡住了方式,因此无法到达 shell 脚本中的下一个命令。 :-)

做这样的事情最好的方法是什么?

【问题讨论】:

  • 这可能是一种可行的方法:stackoverflow.com/questions/22460691/… 通过使用 Maven 阶段 pre-integration-test、integration-test 和 post-integration-test 问题:如何知道使用的 pre-integration-test 阶段当嵌入式 Jetty 最终启动以继续测试时,由 maven-exec-plugin 执行?
  • 你是如何启动 Jetty 的?有一个Jetty Maven Plugin 可以用于这样的事情..?
  • @khmarbaise:问题是它是一个嵌入式 Jetty,它从一个主类开始。我认为这不适合 Jetty Maven 插件,因为这从另一个角度构建了一个 Jetty - 可能是基于 WAR。
  • 问题是你真正喜欢测试什么而且这不是单元测试而是集成测试。

标签: maven jenkins junit automated-tests embedded-jetty


【解决方案1】:

我已经通过以下 maven 构建设置实现了它:

<profiles>
        <profile>
            <id>sendAPI_CI</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jetty</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <async>true</async>
                                    <executable>java</executable>
                                    <arguments>
                                        <argument>-Djson.schemas.location=${project.parent.parent.basedir}/common/target/test/resources/json_schemas/</argument>
                                        <argument>-jar</argument>
                                        <argument>${project.parent.parent.basedir}/facade/target/ams-jar-with-dependencies.jar</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>wait-maven-plugin</artifactId>
                        <version>1.0</version>
                        <executions>
                            <execution>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>wait</goal>
                                </goals>
                                <configuration>
                                    <protocol>http</protocol>
                                    <host>localhost</host>
                                    <port>8181</port>
                                    <file>/swagger</file>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.21.0</version>
                        <executions>
                            <execution>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
                            <testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
                            <includes>
                                <include>com.messaging.async.facade.sendApi.SendApiResourceTest.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

pre-integration-test 阶段,我使用 exec-maven-plugin 来启动(嵌入式)Jetty。然后在 wait-maven-plugin 的帮助下,在同一个 Maven 阶段,我们等到 Jetty 启动,这是通过检查 URL 是否可调用来实现的。最后,在集成测试阶段的 maven-failsafe-plugin 有它的使命,并最终针对 Jetty 开始集成测试。如您所见,这是在 Maven profile 中配置的,因此在 Jenkins 工作中我可以在那里专门使用它。完毕!

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 2019-09-19
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 2015-04-13
    • 2021-12-28
    • 1970-01-01
    相关资源
    最近更新 更多