This is my File system code:
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;
}
Imported Junit 4.12 and springframework mock. but while running junit test getting NullPointer Exception at String archive = (env.getProperty("file.archivePath")) + name;
Accessing path from application.properties. Test code is below.
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();
}
}
}
Stack Trace of Exception:
java.lang.NullPointerException at prj.iopo.filesystem.FileSystemHandler.listVersionRestore(FileSystemHandler.java:231) at prj.iopo.testcase.TestFileSystemHandler.testListVersionRestore(TestFileSystemHandler.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)