java - 属性文件值未出现在我的变量中
问题描述
目前正在学习自动化测试,我无法理解我的代码有什么问题。我正在尝试从 .properties 文件中获取信息并将其用作变量。我收到一个错误:
java.io.FileNotFoundException: ..\resources\config.properties (The system cannot find the path specified)
但是,我确信我的道路是正确的。尝试了不同的变体,例如//resources//config...
甚至使用\\
,仍然是相同的问题。
这是我尝试从 config.properties 文件中获取信息的代码:
@Test
public void myFirstTest() {
LoginPage log = new LoginPage();
try(FileReader reader = new FileReader("../resources/config.properties")) {
Properties properties = new Properties();
properties.load(reader);
String username = (String) properties.get("username");
String password = (String) properties.get("password");
System.out.println("h" + username);
log.insertUsername(username);
log.insertPassword(password);
} catch(Exception e) {
e.printStackTrace();
}
}
这就是我的样子config.properties
username = myUserName1
password = myTestPass1
这是我的文件的架构:
PS我正在尝试从测试中获取源文件->LabelsAndFoldersTest.java
解决方案
您提供的路径config.properties
不正确
正如我可以看到你的项目结构的图像,你的项目src/resources
下有一个文件夹,你有地方config.properties
将文件路径更改为src/resources/config.properties
try(FileReader reader = new FileReader("src/resources/config.properties"))
推荐阅读
- python - 在 Django 中引发异常时,写入日志文件的最佳方法是什么?
- scala - 使用管道分隔符将案例类转换为字符串
- c# - 在调用 SaveChangesAsync() 时,在一对多关系中添加子级会导致异常
- c# - 可以/应该将这三个查询合并为一个吗?
- java - Camera 2 API 问题,图像始终以横向模式显示
- javascript - 为什么我的帖子中没有显示图像?
- java - 如何将按钮放置在线性布局(根视图)中包含的两个线性布局(嵌套视图)下方
- unity3d - 在组件 B 上执行任何事件之前,是否所有 Unity 事件都在组件 A 上执行
- python - 使用 AES 加密时的字节问题
- file-upload - 在vue js,元素ui上传组件中上传之前调整大小/压缩图像