【问题标题】:Jetty 9.3.4 not working with integration testsJetty 9.3.4 不适用于集成测试
【发布时间】:2016-01-06 12:58:40
【问题描述】:

我正在使用 jersey、jetty 9.x、jetty-maven-plugin 和 maven-failsafe-plugin 运行集成测试。

集成测试与 jetty-maven-plugin 中指定的 jetty 9.2.0.M0 配合良好。使用 9.3.4.RC1 版本时,jetty 启动,但不运行集成测试。

这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>my.example</groupId>
  <artifactId>jetty-integration-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>jetty-integration-test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.19</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>jetty-integration-test</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.2.0.M0</version>
        <!--TODO: 9.3.4.RC1 does not verify integration tests as 9.2.0.M0-->
        <configuration>
          <httpConnector>
            <port>8081</port>
          </httpConnector>
          <scanIntervalSeconds>2</scanIntervalSeconds>
          <contextPath>/</contextPath>
          <stopPort>8005</stopPort>
          <stopKey>STOP</stopKey>
        </configuration>
        <executions>
          <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>run</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>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.18</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>integration-test</display-name>
  <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>my.example.jetty_integration_test.App</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

</web-app>

这是一个最小的网络服务示例:
应用程序.java:

package my.example.jetty_integration_test;


import org.glassfish.jersey.server.ResourceConfig;

public class App extends ResourceConfig {
    /**
     * Register JAX-RS application components.
     */
    public App() {
        register(WebService.class);
    }
}

WebService.java:

package my.example.jetty_integration_test;


import ...

// Browse to http://localhost:8081/rest/webservice

@Path("webservice")
public class WebService {


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

还有一个显示问题的最小集成测试:
AppITCase.java

package my.example.jetty_integration_test;

import ...

/**
 * Unit test for simple App.
 */
public class AppITCase {
    CloseableHttpClient httpClient;

    @Before
    public void setUp() {
        httpClient = HttpClients.createDefault();
    }

    @After
    public void tearDown() {
        try {
            httpClient.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void canCallWebService() {
        // Given.
        HttpGet httpGet = new HttpGet("http://localhost:8081/rest/webservice");
        // When.
        CloseableHttpResponse httpResponse = tryHttpRequest(httpGet);
        String text = tryReadHttpBody(httpResponse);
        // Then.
        assertEquals("Got it!", text.trim());

    }

    private CloseableHttpResponse tryHttpRequest(HttpUriRequest httpRequest) {
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpRequest);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
        return httpResponse;
    }

    private String tryReadHttpBody(HttpResponse httpResponse){
        try {
            InputStream inputStream = httpResponse.getEntity().getContent();
            byte[] bytes = new byte[64];
            inputStream.read(bytes, 0, bytes.length);
            return new String(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

在 pom.xml 中的两个 jetty 版本之间切换时,mvn clean verify 将起作用,否则它不会运行任何 IT 案例... 如何使用较新的 jetty 版本运行集成测试?

编辑: jetty 启动后,failsafe 不会向控制台输出任何输出(它与 jetty 9.2.0.M0 一样)。
使用码头 9.3.4.RC1 验证来自 maven clean 的控制台输出:

~/workspace/jetty-integration-test$ mvn clean verify
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jetty-integration-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jetty-integration-test ---
[INFO] Deleting /home/alex/workspace/jetty-integration-test/target
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ jetty-integration-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/alex/workspace/jetty-integration-test/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ jetty-integration-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/alex/workspace/jetty-integration-test/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ jetty-integration-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/alex/workspace/jetty-integration-test/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ jetty-integration-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/alex/workspace/jetty-integration-test/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ jetty-integration-test ---
[INFO] Surefire report directory: /home/alex/workspace/jetty-integration-test/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jetty-integration-test ---
[INFO] Building jar: /home/alex/workspace/jetty-integration-test/target/jetty-integration-test.jar
[INFO] 
[INFO] >>> jetty-maven-plugin:9.3.4.RC1:run (start-jetty) @ jetty-integration-test >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ jetty-integration-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/alex/workspace/jetty-integration-test/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ jetty-integration-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/alex/workspace/jetty-integration-test/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ jetty-integration-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/alex/workspace/jetty-integration-test/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ jetty-integration-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/alex/workspace/jetty-integration-test/target/test-classes
[INFO] 
[INFO] <<< jetty-maven-plugin:9.3.4.RC1:run (start-jetty) @ jetty-integration-test <<<
[INFO] 
[INFO] --- jetty-maven-plugin:9.3.4.RC1:run (start-jetty) @ jetty-integration-test ---
2015-10-08 21:44:42.385:INFO::main: Logging initialized @6255ms
[INFO] Configuring Jetty for project: jetty-integration-test
[INFO] webAppSourceDirectory not set. Trying src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = /home/alex/workspace/jetty-integration-test/target/classes
[INFO] Context path = /
[INFO] Tmp directory = /home/alex/workspace/jetty-integration-test/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:///home/alex/workspace/jetty-integration-test/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = /home/alex/workspace/jetty-integration-test/src/main/webapp
2015-10-08 21:44:42.553:INFO:oejs.Server:main: jetty-9.3.4.RC1
2015-10-08 21:44:45.073:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@4cdb8504{/,file:///home/alex/workspace/jetty-integration-test/src/main/webapp/,AVAILABLE}{file:///home/alex/workspace/jetty-integration-test/src/main/webapp/}
2015-10-08 21:44:45.101:INFO:oejs.ServerConnector:main: Started ServerConnector@25e70455{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2015-10-08 21:44:45.103:INFO:oejs.Server:main: Started @8974ms
[INFO] Started Jetty Server

【问题讨论】:

  • “不会运行任何 IT 案例”是什么意思? Failsafe 运行您的 IT 案例,那么您从 Failsafe 获得的 Maven 输出是什么?你有运行 mvn -X clean verify 吗?
  • 我为上面的 jetty 9.3.4.RC1 添加了控制台输出。 “mvn -X clean verify”也不会从故障安全插件中产生任何输出...
  • 您的码头服务器似乎在非守护程序模式下运行。尝试使用发行版 9.3.4.v20151007 而不是 RC1。
  • Jetty 9.2.0.M0 是一个里程碑版本(如果您愿意,可以使用测试版)。它不是基于任何东西的最终或稳定版本。 Jetty 9.3.4.v20151007 是最新的稳定版本,使用它。
  • 是的,没错。 atm 9.3.4.v2015007 在 repo.maven.apache.org 上不可用,所以我使用了 jetty 9.3.4 的候选发布版本。但是在 9.2.0 中使用里程碑版本是没有意义的

标签: java maven jetty maven-failsafe-plugin


【解决方案1】:

jetty-maven-plugin:run 的使用意味着您正在使用 Jetty 运行您的 &lt;packaging&gt;war&lt;/packaging&gt; 项目。您的项目正在部署并准备好让您使用浏览器(或者实际上,任何不在 Maven 进程内的东西)来访问它。

完成后,您只需 Ctrl+C 即可停止该 Jetty 实例。

也许您正在考虑 jetty-maven-plugin:start(及其关联的 jetty-maven-plugin:stop),它旨在启动 Jetty,而不是阻止等待 Jetty 进程停止(或退出)

https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#jetty-start-goal

【讨论】:

  • 这并不能解释为什么他在 jetty 9.2 中得到了预期的行为
  • 您的经验基于 Jetty 9.2 的不稳定/beta/里程碑版本,当时 maven 插件正在进行大规模重构。将此归结为对错误/意外行为的意外依赖。
  • 在预集成测试阶段指定 start 而不是 run 使用 jetty 9.3.4.RC1 运行集成测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2020-03-16
  • 2018-06-22
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多