问题
I'm running integration tests using jersey, jetty 9.x, jetty-maven-plugin and the maven-failsafe-plugin.
Integration tests worked well with jetty 9.2.0.M0 specified in jetty-maven-plugin. When using version 9.3.4.RC1, jetty starts, but no integration tests are run.
Here's my 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>
Thats a minimal web-service example:
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!";
}
}
And a minimal integration test that shows the problem:
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);
}
}
}
When switching between the two jetty versions in the pom.xml, mvn clean verify will work or it will not run any IT case... How can I run integration tests using the newer jetty version?
Edit:
After jetty started, failsafe does not make any output to the console (It does as usual for jetty 9.2.0.M0).
Console output from maven clean verify with jetty 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:
The use of jetty-maven-plugin:run
means you are running your <packaging>war</packaging>
project with Jetty. Your project is sitting deployed and ready for you to hit it with a Browser (or really, anything not inside of the Maven process).
When you are done you simple Ctrl+C to stop that Jetty instance.
Perhaps you are thinking of jetty-maven-plugin:start
(and its associated jetty-maven-plugin:stop
) which is meant to start Jetty and not block waiting for the Jetty process to be stopped (or exited)
See https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#jetty-start-goal
来源:https://stackoverflow.com/questions/33023669/jetty-9-3-4-not-working-with-integration-tests