无法读取AWS设备场中的属性文件 [英] Can't read properties file in aws device farm
问题描述
我的Appium + JUnit测试在本地运行正常,但是在aws上找不到属性文件.我的测试位于src/test/java
下,属性文件用于src/test/resources/locale
下的测试.
包含相关内容的邮政编码:
My Appium + JUnit tests works perfectly fine locally, but on aws it can not find properties files. My tests are placed under src/test/java
and properties files used in tests under src/test/resources/locale
.
Zip with dependencies content:
├── app-0.1-tests.jar
├── app-0.1.jar
└── dependency-jars
├── ...
app-0.1-tests.jar
内容:
├── META-INF
│ ├── MANIFEST.MF
│ ├── maven
│ ├── ....
├── com
│ ├── ....
└── locale
├── en_EN.properties
不幸的是,当我尝试加载属性文件时,我有一个IOException-在以下位置找不到该文件:
file:/tmp/scratchcC4yMz.scratch/test-packagefQ2euI/app-0.1-tests.jar!/locale/en_EN.properties
Unfortunately when I try to load properties files I have an IOException - file is not found in location:
file:/tmp/scratchcC4yMz.scratch/test-packagefQ2euI/app-0.1-tests.jar!/locale/en_EN.properties
每次遇到相同的问题时,我都尝试以几种方式加载它们.在本地,一切都像魅力.代码示例:
I tried to load them in a couple of ways, each time I have the same issue. Locally everything works like a charm. Example of code:
File file = new File(ClassName.class.getResource("/locale/en_EN.properties").getPath());
try (InputStream inputStream = new FileInputStream(file.getPath());
InputStreamReader streamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"))) {
Properties properties = new Properties();
properties.load(streamReader);
return properties;
} catch (IOException e) {
System.err.println("Properties file not found: " + file.getPath());
}
或使用类加载器:
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource("/locale/en_EN.properties");
有人可以建议解决方案如何读取资源中的属性文件吗?
Can someone suggest solution how to read properties file located in resources?
推荐答案
这是我所做的,并且似乎可以正常工作:
Here is what I did and it seemed to work:
/**
* Rigourous Test :-)
*/
public void testApp()
{
//taken from https://www.mkyong.com/java/java-properties-file-examples/
//create properties object
Properties prop = new Properties();
InputStream input = null;
//load in the properties file from src/test/resources
try {
input = Thread.currentThread().getContextClassLoader().getResourceAsStream("myproperties.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("devicefarm"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是设备场中的java输出:
Here is my java output from device farm:
[TestNG] Running:
Command line suite
helloWorld
这是我的属性文件的内容:
and here is the contents of my properties file:
devicefarm=helloWorld
在我的本地测试中,它似乎也可以工作:
In my local test it seemed to work too:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.devicefarm.www.AppTest
helloWorld
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
看来,在打包属性文件之后,我们无法像在maven中通过本地方式那样在Java中引用它.如果我们尝试使test-jar可执行文件并运行jar,我想同样的事情也会发生.
It would appear that after the properties file is package we can not reference it in java the same way we do locally through maven. If we try to make the test-jar executable and run the jar I think the same thing would happen.
有关类加载器和线程加载器之间的区别,请参见此SO帖子.
see this SO post for difference between the class loader and the thread loader.
https://stackoverflow.com/a/22653795/4358385
关于导出tmp目录,在此页面上,我们可以告诉设备场导出我们想要的任何目录
Regarding exporting the tmp directory, on this page we can tell device farm to export whatever directories we want
然后,当测试完成时,我们可以单击客户工件"链接并获得该导出的压缩文件.
Then when the test is finished we can click on the customer artifacts link and get a zip of that export.
这篇关于无法读取AWS设备场中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!