Spring / Jersey / Grizzly 在 maven 命令行中导致静默测试失败,但在 Eclipse 中成功?
Spring / Jersey / Grizzly causing silent test failure in maven command-line, but succeeds in Eclipse?
我们有一个 JUnit/Jersey 集成测试,当通过 Maven 从命令行 运行 时失败并显示 "Connection Refused"。但是,当来自 Eclipse 的 运行 时,测试通过。似乎 Grizzly 容器没有启动有一些错误,但我找不到任何错误消息或日志,它只是默默地失败了。这是在我们从 Java 7 CentOS 6 环境升级到 Java 8 CentOS 7 环境时发生的。下面是重现此错误的简单测试:
public class CrobichaGrizzlyIntegrationTest extends JerseyTest {
private WebResource webResource;
private static final String PACKAGE_NAME = "xxx";
byte[] bytes;
byte[] expectedResponses;
public CrobichaGrizzlyIntegrationTest() {
super(
new WebAppDescriptor.Builder(PACKAGE_NAME)
.clientConfig(
new DefaultClientConfig()).servletPath("/")
.contextParam("contextConfigLocation", "classpath:app-config.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.build()
);
}
@Before
public void setup() {
bytes = ReputationHelper.createReputationRequest().toByteArray();
expectedResponses = ReputationHelper.createReputationResponse().toByteArray();
}
// 1. valid POST/GET(E2E) request
@Test
public void testPostRequestSuccess() throws Exception {
webResource = resource().path("xyz").queryParam("query", "value");
final byte[] response =
webResource.type(HttpUtils.MIME_APPLICATION_PROTOBUF).accept(HttpUtils.MIME_APPLICATION_PROTOBUF)
.cookie(new Cookie("cookie", "abcd")).post(byte[].class, bytes);
final ResponseList expectedResponseList = ResponseList.parseFrom(expectedResponses);
final ResponseList responseList = ResponseList.parseFrom(response);
Assert.assertEquals(expectedResponseList.getResponse(0), responseList.getResponse(0));
}
}
来自 maven 的错误报告:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="xxx.CrobichaGrizzlyIntegrationTest" time="0" tests="1" errors="1" skipped="0" failures="0">
<properties>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="sun.boot.library.path" value="/usr/java/jdk1.8.0_45/jre/lib/amd64"/>
<property name="java.vm.version" value="25.45-b02"/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
<property name="java.vendor.url" value="http://java.oracle.com/"/>
<property name="path.separator" value=":"/>
<property name="guice.disable.misplaced.annotation.check" value="true"/>
<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
<property name="file.encoding.pkg" value="sun.io"/>
<property name="user.country" value="US"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.os.patch.level" value="unknown"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="user.dir" value="---removed---"/>
<property name="java.runtime.version" value="1.8.0_45-b14"/>
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
<property name="java.endorsed.dirs" value="/usr/java/jdk1.8.0_45/jre/lib/endorsed"/>
<property name="os.arch" value="amd64"/>
<property name="java.io.tmpdir" value="/tmp"/>
<property name="line.separator" value=" "/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="os.name" value="Linux"/>
<property name="classworlds.conf" value="/usr/share/maven/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.class.version" value="52.0"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="os.version" value="3.10.0-229.4.2.el7.x86_64"/>
<property name="user.home" value="/home/chris_robichaud"/>
<property name="user.timezone" value="America/New_York"/>
<property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/>
<property name="file.encoding" value="UTF-8"/>
<property name="java.specification.version" value="1.8"/>
<property name="user.name" value="chris_robichaud"/>
<property name="java.class.path" value="/usr/share/maven/boot/plexus-classworlds.jar"/>
<property name="java.vm.specification.version" value="1.8"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.home" value="/usr/java/jdk1.8.0_45/jre"/>
<property name="sun.java.command" value="org.codehaus.plexus.classworlds.launcher.Launcher integration-test -rf :project_name -X"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
<property name="user.language" value="en"/>
<property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
<property name="java.vm.info" value="mixed mode"/>
<property name="java.version" value="1.8.0_45"/>
<property name="java.ext.dirs" value="/usr/java/jdk1.8.0_45/jre/lib/ext:/usr/java/packages/lib/ext"/>
<property name="sun.boot.class.path" value="/usr/java/jdk1.8.0_45/jre/lib/resources.jar:/usr/java/jdk1.8.0_45/jre/lib/rt.jar:/usr/java/jdk1.8.0_45/jre/lib/sunrsasign.jar:/usr/java/jdk1.8.0_45/jre/lib/jsse.jar:/usr/java/jdk1.8.0_45/jre/lib/jce.jar:/usr/java/jdk1.8.0_45/jre/lib/charsets.jar:/usr/java/jdk1.8.0_45/jre/lib/jfr.jar:/usr/java/jdk1.8.0_45/jre/classes"/>
<property name="java.vendor" value="Oracle Corporation"/>
<property name="maven.home" value="/usr/share/maven"/>
<property name="file.separator" value="/"/>
<property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/>
<property name="sun.cpu.endian" value="little"/>
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="sun.desktop" value="gnome"/>
<property name="sun.cpu.isalist" value=""/>
</properties>
<testcase name="testPostRequestSuccess" classname="xxx.CrobichaGrizzlyIntegrationTest" time="0">
<error message="java.net.ConnectException: Connection refused" type="com.sun.jersey.api.client.ClientHandlerException"><![CDATA[com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1282)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1257)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.getOutputStream(URLConnectionClientHandler.java:238)
at com.sun.jersey.api.client.CommittingOutputStream.commitStream(CommittingOutputStream.java:117)
at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:83)
at com.sun.jersey.core.impl.provider.entity.ByteArrayProvider.writeTo(ByteArrayProvider.java:95)
at com.sun.jersey.core.impl.provider.entity.ByteArrayProvider.writeTo(ByteArrayProvider.java:59)
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access0(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
at xxx.CrobichaGrizzlyIntegrationTest.testPostRequestSuccess(CrobichaGrizzlyIntegrationTest.java:88)
]]></error>
</testcase>
</testsuite>
只是想添加一个答案,以防其他人遇到这个问题:我花了一些时间清理我的 maven pom 文件,它似乎已经修复了。从来没有找到根本原因。
我们有一个 JUnit/Jersey 集成测试,当通过 Maven 从命令行 运行 时失败并显示 "Connection Refused"。但是,当来自 Eclipse 的 运行 时,测试通过。似乎 Grizzly 容器没有启动有一些错误,但我找不到任何错误消息或日志,它只是默默地失败了。这是在我们从 Java 7 CentOS 6 环境升级到 Java 8 CentOS 7 环境时发生的。下面是重现此错误的简单测试:
public class CrobichaGrizzlyIntegrationTest extends JerseyTest {
private WebResource webResource;
private static final String PACKAGE_NAME = "xxx";
byte[] bytes;
byte[] expectedResponses;
public CrobichaGrizzlyIntegrationTest() {
super(
new WebAppDescriptor.Builder(PACKAGE_NAME)
.clientConfig(
new DefaultClientConfig()).servletPath("/")
.contextParam("contextConfigLocation", "classpath:app-config.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.build()
);
}
@Before
public void setup() {
bytes = ReputationHelper.createReputationRequest().toByteArray();
expectedResponses = ReputationHelper.createReputationResponse().toByteArray();
}
// 1. valid POST/GET(E2E) request
@Test
public void testPostRequestSuccess() throws Exception {
webResource = resource().path("xyz").queryParam("query", "value");
final byte[] response =
webResource.type(HttpUtils.MIME_APPLICATION_PROTOBUF).accept(HttpUtils.MIME_APPLICATION_PROTOBUF)
.cookie(new Cookie("cookie", "abcd")).post(byte[].class, bytes);
final ResponseList expectedResponseList = ResponseList.parseFrom(expectedResponses);
final ResponseList responseList = ResponseList.parseFrom(response);
Assert.assertEquals(expectedResponseList.getResponse(0), responseList.getResponse(0));
}
}
来自 maven 的错误报告:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="xxx.CrobichaGrizzlyIntegrationTest" time="0" tests="1" errors="1" skipped="0" failures="0">
<properties>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="sun.boot.library.path" value="/usr/java/jdk1.8.0_45/jre/lib/amd64"/>
<property name="java.vm.version" value="25.45-b02"/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
<property name="java.vendor.url" value="http://java.oracle.com/"/>
<property name="path.separator" value=":"/>
<property name="guice.disable.misplaced.annotation.check" value="true"/>
<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
<property name="file.encoding.pkg" value="sun.io"/>
<property name="user.country" value="US"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.os.patch.level" value="unknown"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="user.dir" value="---removed---"/>
<property name="java.runtime.version" value="1.8.0_45-b14"/>
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
<property name="java.endorsed.dirs" value="/usr/java/jdk1.8.0_45/jre/lib/endorsed"/>
<property name="os.arch" value="amd64"/>
<property name="java.io.tmpdir" value="/tmp"/>
<property name="line.separator" value=" "/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="os.name" value="Linux"/>
<property name="classworlds.conf" value="/usr/share/maven/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.class.version" value="52.0"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="os.version" value="3.10.0-229.4.2.el7.x86_64"/>
<property name="user.home" value="/home/chris_robichaud"/>
<property name="user.timezone" value="America/New_York"/>
<property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/>
<property name="file.encoding" value="UTF-8"/>
<property name="java.specification.version" value="1.8"/>
<property name="user.name" value="chris_robichaud"/>
<property name="java.class.path" value="/usr/share/maven/boot/plexus-classworlds.jar"/>
<property name="java.vm.specification.version" value="1.8"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.home" value="/usr/java/jdk1.8.0_45/jre"/>
<property name="sun.java.command" value="org.codehaus.plexus.classworlds.launcher.Launcher integration-test -rf :project_name -X"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
<property name="user.language" value="en"/>
<property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
<property name="java.vm.info" value="mixed mode"/>
<property name="java.version" value="1.8.0_45"/>
<property name="java.ext.dirs" value="/usr/java/jdk1.8.0_45/jre/lib/ext:/usr/java/packages/lib/ext"/>
<property name="sun.boot.class.path" value="/usr/java/jdk1.8.0_45/jre/lib/resources.jar:/usr/java/jdk1.8.0_45/jre/lib/rt.jar:/usr/java/jdk1.8.0_45/jre/lib/sunrsasign.jar:/usr/java/jdk1.8.0_45/jre/lib/jsse.jar:/usr/java/jdk1.8.0_45/jre/lib/jce.jar:/usr/java/jdk1.8.0_45/jre/lib/charsets.jar:/usr/java/jdk1.8.0_45/jre/lib/jfr.jar:/usr/java/jdk1.8.0_45/jre/classes"/>
<property name="java.vendor" value="Oracle Corporation"/>
<property name="maven.home" value="/usr/share/maven"/>
<property name="file.separator" value="/"/>
<property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/>
<property name="sun.cpu.endian" value="little"/>
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="sun.desktop" value="gnome"/>
<property name="sun.cpu.isalist" value=""/>
</properties>
<testcase name="testPostRequestSuccess" classname="xxx.CrobichaGrizzlyIntegrationTest" time="0">
<error message="java.net.ConnectException: Connection refused" type="com.sun.jersey.api.client.ClientHandlerException"><![CDATA[com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1282)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1257)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.getOutputStream(URLConnectionClientHandler.java:238)
at com.sun.jersey.api.client.CommittingOutputStream.commitStream(CommittingOutputStream.java:117)
at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:83)
at com.sun.jersey.core.impl.provider.entity.ByteArrayProvider.writeTo(ByteArrayProvider.java:95)
at com.sun.jersey.core.impl.provider.entity.ByteArrayProvider.writeTo(ByteArrayProvider.java:59)
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access0(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
at xxx.CrobichaGrizzlyIntegrationTest.testPostRequestSuccess(CrobichaGrizzlyIntegrationTest.java:88)
]]></error>
</testcase>
</testsuite>
只是想添加一个答案,以防其他人遇到这个问题:我花了一些时间清理我的 maven pom 文件,它似乎已经修复了。从来没有找到根本原因。