我正在嘗試使用Spring 3編寫用於處理文件上傳控制器的單元測試。現在,如果通過控制器將圖像發送到我的服務方法,那麼一切正常。但是當做一個單元測試時,我得到一個空指針異常。創建DiskFileItem時發生java.lang.NullPointerException
看起來DiskFilteItem
裏面的屬性「dfos」是空的,當我手動實例化它時,但是當它從控制器中檢索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)
你是對的,這是描述中的一個錯字,我將原始示例修改爲指向一個文件。 日誌文件條目顯示以下內容 2010-11-08 11:57:11,907 [main] INFO ClientTest - 找到文件:true 2010-11-08 11:57:11,908 [main] INFO ClientTest - 文件大小:291812 – 2010-11-08 16:58:04
如果它解決了您的問題,請註冊並將其標記爲答案。真的很感激它。 – 2010-11-08 20:43:39