When I run JUnit tests using ClasspathSuite and one of the unit tests wants to load a file using a relative path, it will use the base path of ClasspathSuite instead of the base path of the unit test.
So for example if the unit test tries to load a file like this:
File file = new File("src/test/resources/test.xml");
it will try to load that file relative to the location of ClasspathSuite and not relative to the location of the unit test.
Is there any way to change this?
I assume you are using eclipse. Use MYClass.class.getClassLoader().getResourceAsStream(<fileName>)
. As it is in resources getClassLoader()
will be able to get the location of the file easily, if you are using eclipse. Your code would be,
File file = new File(MYClass.class.getClassLoader().getResourceAsStream("test.xml"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.