我需要在启动服务器时加载文件资源。所以我把文件放在 webapp/WEB-INF 下。
问题是单元测试失败。
我尝试使用以下方法加载文件:
URL url = Utils.class.getClassLoader().getResource(fileName);
File file = new File(url.getFile());
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
但是类加载器没有找到该文件。它似乎正在寻找它
file:/C:/<local path>/<my project>/target/test-classes/
- 在正常模式和测试模式期间有不同的上下文
我也尝试了这里建议的解决方案,但没有奏效