我正在尝试使用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)
答案 0 :(得分:8)
我遇到了同样的问题。问题是,DiskFileItem.getSize()
在时间上与DiskFileItem.getOutputStream()
结合,因为内部字段在getOutputStream
中初始化并在getSize
中使用。
解决方案是
final File TEST_FILE = new File("src/test/resources/test.jpg");
final DiskFileItem diskFileItem = new DiskFileItem("file", "image/jpeg", true, TEST_FILE.getName(), 100000000, TEST_FILE.getParentFile());
diskFileItem.getOutputStream();
在将diskFileItem
传递给CommonsMultipartFile
的构造函数之前。
答案 1 :(得分:0)
这个日志的这一部分是什么意思?
log.info("found file: " +file.exists());
log.info("file size: " +file.length());
但我认为问题可能是因为:
File file = new File("//Users//test//Downloads//");
它看起来像是指向一个目录而不是一个文件,所以也许这就是为什么当你想获得NullPointerException
DiskFileItem
的原因
答案 2 :(得分:0)
如果上面的解决方案都不适合你,就像我发生的那样:)只需抛弃 DiskFileItem 并使用下面的解决方案:
final File fileToUpload = new File("src/test/resources/files/test.txt");
final MultiValueMap<String, Object> request = new LinkedMultiValueMap<String, Object>();
request.add("file", new FileSystemResource(fileToUpload.getAbsolutePath()));
答案 3 :(得分:0)
虽然与问题不完全相关。在向GAE部署一些文件上传代码时遇到了同样的异常。我从这里修改了systempuntoout的代码using apache fileupload on GAE(它使用了apache commons的stream部分),然后工作正常。
答案 4 :(得分:-1)
new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, file);
会产生null
值。查看docs以查看错误或者您将某些null
作为参数传递