Java – files cannot be accessed from Src / main / resources through test cases
•
Java
I have a file in Src / main / resources dat.
When I try to test a class that loads the file through a jar file, the test fails because it cannot find the file in the path (I / O exception) The path obtained through the test is:
/home/usr/workspace/project/target/test-classes/file. dat
But the files in the target / test class don't have any ideas?
Solution
Files from Src / main / resources will be available in the classpath when the main program runs, while files from Src / main / resources and Src / test / resources will be available in the classpath during the test run
One way to retrieve files located on the classpath is:
Object content = Thread.currentThread().getContextClassLoader().getResource("file.dat").getContent();
.. The content type depends on the file content You can also get the file as InputStream:
InputStream contentStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.dat");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码