我正在测试使用dropwizard构建的端点,但出了点问题:
@Test
public void testPostWithFileSuccess() throws Exception
{
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(fixture("resources/testImage.jpg")));
filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("testImage.jpg").build());
FormDataMultiPart request = new FormDataMultiPart();
request.field("data", fixture("resources/postWithFileSuccess.json"), MediaType.APPLICATION_JSON_TYPE);
request.bodyPart(filePart);
WebTarget target = mClient.target("http://localhost:8080/crackers").register(MultiPartFeature.class);
Response response = target
.request()
.post(Entity.entity(request, MediaType.MULTIPART_FORM_DATA));
assertThat(response.getStatus()).isEqualTo(200);
}
实际发生的是.post(Entity.entity(request, MediaType.MULTIPART_FORM_DATA));
上的测试失败,并进行了以下考察:
javax.ws.rs.ProcessingException:java.io.FileNotFoundException:文件路径无效 在org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:287) 在org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252) 在org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:684) 在org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:681) 在org.glassfish.jersey.internal.Errors.process(Errors.java:315) 在org.glassfish.jersey.internal.Errors.process(Errors.java:297) 在org.glassfish.jersey.internal.Errors.process(Errors.java:228) 在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444) 在org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681) 在org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:437) 在org.glassfish.jersey.client.JerseyInvocation $ Builder.post(JerseyInvocation.java:343) 在org.example.project.resources.CrackersTest.testPostWithFileSuccess(CrackersTest.java:201) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在org.junit.rules.ExternalResource $ 1.evaluate(ExternalResource.java:48) 在org.junit.rules.RunRules.evaluate(RunRules.java:20) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229) 在org.junit.runners.ParentRunner.run(ParentRunner.java:309) 在org.junit.runner.JUnitCore.run(JUnitCore.java:160) 在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) 在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 引起:java.io.FileNotFoundException:文件路径无效 在java.io.FileInputStream。(FileInputStream.java:133) 在org.glassfish.jersey.message.internal.FileProvider.writeTo(FileProvider.java:115) 在org.glassfish.jersey.message.internal.FileProvider.writeTo(FileProvider.java:67) 在org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:232) 在org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:79) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250) 在org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 在org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130) 在org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:517) 在org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:499) 在org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388) 在org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285) ......还有42个
我不完全确定,好像该文件不存在或路径错误,Exception
声明会引发new File(...)
。
filePart
显然有问题,但是什么?如果我对request.bodyPart(filePart);
发表评论,那么测试就会失败并出现比较错误(预计会出现这种情况)。
答案 0 :(得分:0)
问题是fixture(...)
方法返回Resources.toString(Resources.getResource(filename), charset).trim()
,因此调试器会看到该文件,但post(...)
无法处理它。
解决问题,而不是
new File(fixture("resources/testImage.jpg"))
我应该用
new File(Resources.getResource("resources/testImage.jpg").toURI())
完整的固定方法列表:
@Test
public void testPostWithFileSuccess() throws Exception
{
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(Resources.getResource("resources/testImage.jpg").toURI()));
filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("testImage.jpg").build());
FormDataMultiPart request = new FormDataMultiPart();
request.field("data", fixture("resources/postWithFileSuccess.json"), MediaType.APPLICATION_JSON_TYPE);
request.bodyPart(filePart);
WebTarget target = mClient.target("http://localhost:8080/crackers").register(MultiPartFeature.class);
Response response = target
.request()
.post(Entity.entity(request, MediaType.MULTIPART_FORM_DATA));
assertThat(response.getStatus()).isEqualTo(200);
}