2

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)

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
john
  • 109
  • 1
  • 1
  • 7
  • Where is the StackTrace of NullPointerException? – Mahendra Apr 04 '16 at 12:15
  • I have edited and added stacktrace – john Apr 04 '16 at 12:21
  • Hmm, the NPE is in the `listVersionRestore`-method, but the code-snippet you've posted is the `testBackup`-method.. :S – Kevin Cruijssen Apr 04 '16 at 12:24
  • Check line#101 of `FileSystemHandler.java` for null value. – Mahendra Apr 04 '16 at 12:25
  • 2
    Your test instantiates `fsh` via `new FileSystemHandler()`, so Spring is not involved in creating the object - `env` is null as the (presumed, since invisible) `@Autowired` annotation is effectively useless. You'll have to use a Spring-aware JUnit runner. – kryger Apr 04 '16 at 12:25

0 Answers0