Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用文件系统和spring引导进行Junit测试时出现空指针异常_Java_Spring_Junit_Nullpointerexception_Zipfile - Fatal编程技术网

Java 使用文件系统和spring引导进行Junit测试时出现空指针异常

Java 使用文件系统和spring引导进行Junit测试时出现空指针异常,java,spring,junit,nullpointerexception,zipfile,Java,Spring,Junit,Nullpointerexception,Zipfile,这是我的文件系统代码: public String fileBackup(byte[] fileContent, String name) { String status = "Created file..."; String file = (env.getProperty("file.path"))+ name; File srcfile = new File(file); try { String

这是我的文件系统代码:

  public String fileBackup(byte[] fileContent, String name) {
        String status = "Created file...";
        String file = (env.getProperty("file.path"))+ name;
        File srcfile = new File(file);
        try {
             String archive = (env.getProperty("file.archivePath")) + name;
             File destFile = new File(archive);
             //System.out.println(destFile);
             if (destFile.exists()) {
                  return "alreadyExists";
             } else {
                 destFile.createNewFile();
                  BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(destFile));
                  stream.write(fileContent);
                  stream.close();
                  srcfile.delete();
             }
        } catch (IOException ex) {
             status = "Failed to create file...";
             Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return status;
  }
导入JUnit4.12和springframework模拟。但是在运行junit测试时,在
stringarchive=(env.getProperty(“file.archivePath”)+name从application.properties访问路径。测试代码如下

import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.mock.web.*;
import prj.iopo.filesystem.FileSystemHandler;

public class TestFileSystemHandler {
    FileSystemHandler fsh = new FileSystemHandler();
@Test
    public void testBackup() {
        FileInputStream inputFile;
        try {
            inputFile = new FileInputStream("D:\\LAB\\job\\8972067.zip");
            MockMultipartFile file = new MockMultipartFile("file", "8972067.zip", "application/zip", inputFile);
            byte[] fileContent = file.getBytes();
            String name = file.getOriginalFilename();
            String backupOutput = fsh.fileBackup(fileContent, name);
            assertEquals("Backup Created", backupOutput);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
异常的堆栈跟踪:

位于的java.lang.NullPointerException FileSystemHandler.listVersionRestore(FileSystemHandler.java:231) 在 TestFileSystemHandler.testListVersionRestore(TestFileSystemHandler.java:20) 位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(未知源代码)位于 org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)位于 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)位于 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)位于 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)位于 org.junit.runners.ParentRunner.run(ParentRunner.java:363)位于 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


NullPointerException的StackTrace在哪里?我已经编辑并添加了stacktraceHmm,NPE在
listVersionRestore
-方法中,但是您发布的代码片段是
testBackup
-方法..:将
FileSystemHandler.java
的第101行设为空值。您的测试通过
newfilesystemhandler()
实例化
fsh
,因此Spring不参与创建对象-
env
为空,因为(假定,因为不可见)
@Autowired
注释实际上是无用的。您必须使用支持Spring的JUnit runner。