Jetty 9.3.4不使用集成测试

时间:2015-10-08 18:48:41

标签: java maven jetty maven-failsafe-plugin

我正在使用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>

这是一个最小的网络服务示例:
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启动之后,故障保护不会向控制台输出任何信息(它通常用于码头9.2.0.M0)。
来自maven clean的控制台输出验证码头9.3.4.RC1:

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

1 个答案:

答案 0 :(得分:1)

使用jetty-maven-plugin:run表示您正在使用Jetty运行<packaging>war</packaging>项目。您的项目已经部署并准备好让您使用浏览器(或者实际上,任何不在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