我正在尝试使用Spring 3编写一个单元测试来处理文件上传控制器 . 现在如果我通过控制器将图像发送到我的服务方法,一切正常 . 但是在进行直接单元测试时,我得到一个空指针异常 .
当我手动实例化它时, DiskFilteItem
内的属性"dfos"似乎为null,但是当从控制器检索 MultipartFile
时它被填充 .
File file = new File("//Users//test//Downloads//testimage.jpg");
log.info("found file: " +file.exists());
log.info("file size: " +file.length());
String fieldName = "field";
String contentType = "image/jpeg";
boolean isFormField = false;
String fileName = "testimage.jpg";
int sizeThreshold = 10240;
DiskFileItemFactory factory = new DiskFileItemFactory();
DiskFileItemFactory factory = new DiskFileItemFactory();
// throws null pointer
FileItem fi = factory.createItem(fieldName,contentType,isFormField,fileName);
// so does this one
DiskFileItem item = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, file);
MultipartFile f = new CommonsMultipartFile(item);
我觉得我在设置中遗漏了一些愚蠢的东西 . 我的pom文件包含以下依赖项 .
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0</version>
</dependency>
此代码抛出以下堆栈跟踪
java.lang.NullPointerException
at org.apache.commons.fileupload.disk.DiskFileItem.getSize(DiskFileItem.java:316)
at org.springframework.web.multipart.commons.CommonsMultipartFile.(CommonsMultipartFile.java:60)
at ImgurClientTest.testUploadImage(ImgurClientTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
5 回答
我遇到了同样的问题 . 问题是
DiskFileItem.getSize()
在时间上与DiskFileItem.getOutputStream()
耦合,因为内部字段在getOutputStream
中初始化并在getSize
中使用 .解决方案是做
在将
diskFileItem
传递给CommonsMultipartFile
的构造函数之前 .这部分日志说的是什么?
但我认为问题可能是因为:
它看起来像指向一个目录而不是一个文件,所以也许这就是为什么当你想获得
DiskFileItem
的大小时你得到一个NullPointerException
如果上面的解决方案都不适合你,因为它发生在我身上:)只需抛弃 DiskFileItem 并使用以下解决方案:
虽然与问题并不完全相关 . 在向GAE部署一些文件上传代码时遇到了同样的异常 . 我从这里修改了systempuntoout的代码using apache fileupload on GAE(它使用了apache commons的stream部分)然后工作正常 .
得到一个
null
值 . 查看docs以查看错误或者您可能会传递一些null
作为参数