java - 从一个类而不是另一个类中读取 Java 中的属性文件时没有这样的文件或目录
问题描述
我正在尝试从此路径中读取关于存储库根目录的属性文件夹:
rest/src/main/resources/cognito.properties
我有一个CognitoData
来自此路径的类:rest/src/main/java/com/bitorb/admin/webapp/security/cognito/CognitoData.java
它使用此代码加载 Properties 文件夹,并且运行良好:
new CognitoProperties().loadProperties("rest/src/main/resources/cognito.properties");
@Slf4j
public class CognitoProperties {
public Properties loadProperties(String fileName) {
Properties cognitoProperties = new Properties();
try {
@Cleanup
FileInputStream fileInputStream = new FileInputStream(fileName);
cognitoProperties.load(fileInputStream);
} catch (IOException e) {
log.error("Error occured. Exception message was [" + e.getMessage() + "]");
}
return cognitoProperties;
}
}
但是,当我CognitoData
从位于 的测试类中调用时rest/src/test/java/com/bitorb/admin/webapp/security/cognito/CognitoServiceTest.java
,我收到此错误:
[rest/src/main/resources/cognito.properties (No such file or directory)]
任何人都可以阐明为什么会这样吗?
解决方案
我不知道您使用什么进行测试,但我怀疑您运行测试时的工作目录不是项目根目录。
一种解决方案是改用绝对路径:
/absolute/path/to/project/rest/src/main/resources/cognito.properties
或者可能在测试期间检查工作目录是什么,看看是否可以将其更改为项目根目录。
推荐阅读
- java - Gradle 的 Maven 发布插件未将 POM 或正确版本发布到 Maven 本地
- tableau-api - 将日期范围过滤器的最大值设为今天的日期
- sql - 如何以我想要的格式获取此时间戳,Oracle SQL
- visual-studio-code - 如何让 VSCode 从“转到文件”对话框中“作为新文件打开”
- python - 用于未知数量的参数和未知名称的 argparse
- javascript - 从外部窗口放置事件仅触发第二次
- java - 让tomcat溢出系统属性
- c++ - 在 gtest 中参数化单个测试
- javascript - React redux 组件之间的共享对象
- ios - 从多个联系人的联系人发送短信失败