使用 Play Framework Java 在 Linux/OSX 环境中执行测试时执行异常 InvocationTargetException: null
Execution exception InvocationTargetException: null when executing the tests in Linux/OSX environment using Play Framework Java
我正在使用 Play Framework 2。4.x Java 作为要求的一部分,我需要从外部服务中提取数据。为了解决这个问题,我使用了 WSClient API。我确实按照 Play Framework 文档中提到的步骤测试 Web 服务客户端。 :
https://playframework.com/documentation/2.4.x/JavaTestingWebServiceClients#Returning-files
唯一的区别是我返回 xml 而不是 JSON。当我在 Windows 环境下 运行 时,测试工作正常,但在 Linux 或 Mac 环境下无法执行相同的测试。如果你们中的任何人有一些解决方案,请告诉我。异常详情如下。
异常详情:
@6pc5ioc7f - Internal server error, for (GET) [/newsfeed.xml] ->
play.api.http.HttpErrorHandlerExceptions$$anon: Execution exception[[InvocationTargetException: null]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:265) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[play_2.11-2.4.3.jar:2.4.3]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:158) [play-netty-server_2.11-2.4.3.jar:2.4.3]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:155) [play-netty-server_2.11-2.4.3.jar:2.4.3]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.7.jar:na]
at scala.util.Failure$$anonfun$recover.apply(Try.scala:216) [scala-library-2.11.7.jar:na]
at scala.util.Try$.apply(Try.scala:192) [scala-library-2.11.7.jar:na]
at scala.util.Failure.recover(Try.scala:216) [scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.7.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.7.jar:na]
at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.7.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.7.jar:na]
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at play.routing.RouterBuilderHelper$$anonfun$build$$anonfun$apply$$anonfun.apply(RouterBuilderHelper.scala:66) ~[play-java_2.11-2.4.3.jar:2.4.3]
at play.routing.RouterBuilderHelper$$anonfun$build$$anonfun$apply$$anonfun.apply(RouterBuilderHelper.scala:62) ~[play-java_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$.invokeBlock(Action.scala:533) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$.invokeBlock(Action.scala:530) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.ActionBuilder$$anon.apply(Action.scala:493) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[play_2.11-2.4.3.jar:2.4.3]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.Option.map(Option.scala:146) ~[scala-library-2.11.7.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.7.jar:na]
... 6 common frames omitted
Caused by: java.lang.NullPointerException: null
at play.api.mvc.Results$Status.sendResource(Results.scala:451) ~[play_2.11-2.4.3.jar:2.4.3]
at play.mvc.Results$StatusHeader.sendResource(Results.java:1390) ~[play_2.11-2.4.3.jar:2.4.3]
at play.mvc.Results$StatusHeader.sendResource(Results.java:1379) ~[play_2.11-2.4.3.jar:2.4.3]
at play.mvc.Results$StatusHeader.sendResource(Results.java:1357) ~[play_2.11-2.4.3.jar:2.4.3]
at com.bovotech.subscription.provider.ScienceFeedTest.lambda$init[=12=](ScienceFeedTest.java:32) ~[test-classes/:na]
... 31 common frames omitted
[error] - application - Error
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"html"). Expected elements are <{}rss>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153) ~[na:1.8.0_72]
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) ~[na:1.8.0_72]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) ~[na:1.8.0_72]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204) ~[na:1.8.0_72]
at com.bovotech.subscription.provider.ScienceFeed.lambda$get[=12=](ScienceFeed.java:36) ~[classes/:na]
at play.core.j.FPromiseHelper$$anonfun$map.apply(FPromiseHelper.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.util.Success$$anonfun$map.apply(Try.scala:237) ~[scala-library-2.11.7.jar:na]
at scala.util.Try$.apply(Try.scala:192) ~[scala-library-2.11.7.jar:na]
at scala.util.Success.map(Try.scala:237) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$map.apply(Future.scala:235) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$map.apply(Future.scala:235) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) ~[scala-library-2.11.7.jar:na]
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:40) ~[play_2.11-2.4.3.jar:2.4.3]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) ~[akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library-2.11.7.jar:na]
JUnit 测试
public class ScienceFeedTest {
WSClient wsClient;
Server server;
private ScienceFeed scienceFeed;
@Before
public void init() {
Router router = new RoutingDsl()
.GET("/newsfeed.xml").routeTo(() ->
ok().sendResource("sciencecloud.xml")
)
.build();
server = Server.forRouter(router);
wsClient = WS.newClient(server.httpPort());
scienceFeed = new ScienceFeed();
scienceFeed.setServiceEndPoint("/newsfeed.xml");
scienceFeed.setClient(wsClient);
}
@Test
public void testThatCallingGetWillAlwaysReturnFeedResponse() throws JAXBException {
F.Promise<FeedResponse> feedResponse = scienceFeed.get();
Assert.assertNotNull(feedResponse);
Assert.assertEquals(FeedResponse.class, feedResponse.get(10000).getClass());
}
看来 "newsfeed.xml" 中的 XML 是错误的。就像你需要一个 "rss" 标签,但它在那个地方是 "html" 标签。
请仔细检查 - 此文件与 windows 上的相同。
我找到了这个问题的解决方案。当我使用“sbt test”命令时,我也能够在 Windows 机器上重现该问题。实际问题是,SendResource() 无法加载 xml 文件。那是因为,xml文件直接放在了test目录下。我将 xml 文件移动到 test/resource 目录,问题得到解决
我正在使用 Play Framework 2。4.x Java 作为要求的一部分,我需要从外部服务中提取数据。为了解决这个问题,我使用了 WSClient API。我确实按照 Play Framework 文档中提到的步骤测试 Web 服务客户端。 : https://playframework.com/documentation/2.4.x/JavaTestingWebServiceClients#Returning-files 唯一的区别是我返回 xml 而不是 JSON。当我在 Windows 环境下 运行 时,测试工作正常,但在 Linux 或 Mac 环境下无法执行相同的测试。如果你们中的任何人有一些解决方案,请告诉我。异常详情如下。
异常详情:
@6pc5ioc7f - Internal server error, for (GET) [/newsfeed.xml] ->
play.api.http.HttpErrorHandlerExceptions$$anon: Execution exception[[InvocationTargetException: null]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:265) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[play_2.11-2.4.3.jar:2.4.3]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:158) [play-netty-server_2.11-2.4.3.jar:2.4.3]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:155) [play-netty-server_2.11-2.4.3.jar:2.4.3]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.7.jar:na]
at scala.util.Failure$$anonfun$recover.apply(Try.scala:216) [scala-library-2.11.7.jar:na]
at scala.util.Try$.apply(Try.scala:192) [scala-library-2.11.7.jar:na]
at scala.util.Failure.recover(Try.scala:216) [scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.7.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.7.jar:na]
at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.7.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.7.jar:na]
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at play.routing.RouterBuilderHelper$$anonfun$build$$anonfun$apply$$anonfun.apply(RouterBuilderHelper.scala:66) ~[play-java_2.11-2.4.3.jar:2.4.3]
at play.routing.RouterBuilderHelper$$anonfun$build$$anonfun$apply$$anonfun.apply(RouterBuilderHelper.scala:62) ~[play-java_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$.invokeBlock(Action.scala:533) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$.invokeBlock(Action.scala:530) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.ActionBuilder$$anon.apply(Action.scala:493) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[play_2.11-2.4.3.jar:2.4.3]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.Option.map(Option.scala:146) ~[scala-library-2.11.7.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.7.jar:na]
... 6 common frames omitted
Caused by: java.lang.NullPointerException: null
at play.api.mvc.Results$Status.sendResource(Results.scala:451) ~[play_2.11-2.4.3.jar:2.4.3]
at play.mvc.Results$StatusHeader.sendResource(Results.java:1390) ~[play_2.11-2.4.3.jar:2.4.3]
at play.mvc.Results$StatusHeader.sendResource(Results.java:1379) ~[play_2.11-2.4.3.jar:2.4.3]
at play.mvc.Results$StatusHeader.sendResource(Results.java:1357) ~[play_2.11-2.4.3.jar:2.4.3]
at com.bovotech.subscription.provider.ScienceFeedTest.lambda$init[=12=](ScienceFeedTest.java:32) ~[test-classes/:na]
... 31 common frames omitted
[error] - application - Error
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"html"). Expected elements are <{}rss>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153) ~[na:1.8.0_72]
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:na]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243) ~[na:1.8.0_72]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) ~[na:1.8.0_72]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) ~[na:1.8.0_72]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204) ~[na:1.8.0_72]
at com.bovotech.subscription.provider.ScienceFeed.lambda$get[=12=](ScienceFeed.java:36) ~[classes/:na]
at play.core.j.FPromiseHelper$$anonfun$map.apply(FPromiseHelper.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.util.Success$$anonfun$map.apply(Try.scala:237) ~[scala-library-2.11.7.jar:na]
at scala.util.Try$.apply(Try.scala:192) ~[scala-library-2.11.7.jar:na]
at scala.util.Success.map(Try.scala:237) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$map.apply(Future.scala:235) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$map.apply(Future.scala:235) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) ~[scala-library-2.11.7.jar:na]
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:40) ~[play_2.11-2.4.3.jar:2.4.3]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) ~[akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library-2.11.7.jar:na]
JUnit 测试
public class ScienceFeedTest {
WSClient wsClient;
Server server;
private ScienceFeed scienceFeed;
@Before
public void init() {
Router router = new RoutingDsl()
.GET("/newsfeed.xml").routeTo(() ->
ok().sendResource("sciencecloud.xml")
)
.build();
server = Server.forRouter(router);
wsClient = WS.newClient(server.httpPort());
scienceFeed = new ScienceFeed();
scienceFeed.setServiceEndPoint("/newsfeed.xml");
scienceFeed.setClient(wsClient);
}
@Test
public void testThatCallingGetWillAlwaysReturnFeedResponse() throws JAXBException {
F.Promise<FeedResponse> feedResponse = scienceFeed.get();
Assert.assertNotNull(feedResponse);
Assert.assertEquals(FeedResponse.class, feedResponse.get(10000).getClass());
}
看来 "newsfeed.xml" 中的 XML 是错误的。就像你需要一个 "rss" 标签,但它在那个地方是 "html" 标签。
请仔细检查 - 此文件与 windows 上的相同。
我找到了这个问题的解决方案。当我使用“sbt test”命令时,我也能够在 Windows 机器上重现该问题。实际问题是,SendResource() 无法加载 xml 文件。那是因为,xml文件直接放在了test目录下。我将 xml 文件移动到 test/resource 目录,问题得到解决