Resource files not found from JUnit test cases

前端 未结 6 2091
无人及你
无人及你 2020-12-24 04:45

Summary

My JUnit tests are not finding the files they require during execution. I\'m using Maven for dependency management and compilation.

6条回答
  •  感情败类
    2020-12-24 05:19

    My mistake, the resource files WERE actually copied to target/test-classes. The problem seemed to be due to spaces in my project name, e.g. Project%20Name.

    I'm now loading the file as follows and it works:

    org.apache.commons.io.FileUtils.toFile(myClass().getResource("resourceFile.txt")‌​);
    

    Or, (taken from Java: how to get a File from an escaped URL?) this may be better (no dependency on Apache Commons):

    myClass().getResource("resourceFile.txt")‌​.toURI();
    

提交回复
热议问题