java 如何从junit-tests中的.properties文件中读取字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13699153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read string from .properties file in junit-tests?
提问by Max_Salah
I use wicket in my webapplication. I save the Strings in some .properties files as follows:
我在我的网络应用程序中使用检票口。我将字符串保存在一些 .properties 文件中,如下所示:
foo.properties
foo.properties
page.label=dummy
In the html-file, I can acces the String page.label
as follows:
在 html 文件中,我可以page.label
按如下方式访问字符串:
index.html
索引.html
<wicket:message key="page.label">Default label</wicket:message>
Now I wrote some junit test cases for my Application and would like to access the Strings saved in the properties file. My Question is, how to read the String from the properties file?
现在我为我的应用程序编写了一些 junit 测试用例,并想访问保存在属性文件中的字符串。我的问题是,如何从属性文件中读取字符串?
回答by Festus Tamakloe
Try this
试试这个
import java.io.FileInputStream;
import java.util.Properties;
public class MainClass {
public static void main(String args[]) throws IOException {
Properties p = new Properties();
p.load(new FileInputStream("foo.properties"));
Object label = p.get("page.label");
System.out.println(label);
}
}
This section allow you to read all properties files from wherever you want and load them in the Properties
此部分允许您从任何地方读取所有属性文件并将它们加载到属性
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class MainClass {
private static String PROPERTIES_FILES_PATHNAME = "file:///Users/ftam/Downloads/test/";// for mac
public static void main(String args[]) throws Exception {
Properties p = new Properties();
List<File> files = getFiles();
for(File file : files) {
FileInputStream input = new FileInputStream(file);
p.load(input);
}
String label = (String) p.get("page.label");
System.out.println(label);
}
private static List<File> getFiles() throws IOException, URISyntaxException {
List<File> filesList = new ArrayList<File>();
URL[] url = { new URL(PROPERTIES_FILES_PATHNAME) };
URLClassLoader loader = new URLClassLoader(url);
URL[] urls = loader.getURLs();
File fileMetaInf = new File(urls[0].toURI());
File[] files = fileMetaInf.listFiles();
for(File file : files) {
if(!file.isDirectory() && file.getName().endsWith(".properties")) {
filesList.add(file);
}
}
return filesList;
}
}
回答by bert
Wicket has its own way of localizing the resource, taking into account the component tree. See the javadoc for the StringResourceLoader.
考虑到组件树,Wicket 有自己的本地化资源的方式。请参阅StringResourceLoader 的 javadoc。
One way of loading the Resource would be:
加载资源的一种方法是:
WicketTester tester = new WicketTester(new MyApplication());
tester.startPage(MyPage.class);
Localizer localizer = tester.getApplication().getResourceSettings()
.getLocalizer();
String foo = localizer.getString("page.label",tester.getLastRenderedPage(), "")
回答by Jean Logeart
Using Apache Commons Configurationis a pretty good choice!
使用Apache Commons Configuration是一个不错的选择!
回答by Giannis
Have this field inside your class:
在你的班级中有这个字段:
import java.util.ResourceBundle;
private static ResourceBundle settings = ResourceBundle.getBundle("test",Locale.getDefault());
then a test.properties
file like this:
然后是test.properties
这样的文件:
com.some.name=someValueHere
Finally you can access the property values this way:
最后,您可以通过以下方式访问属性值:
private String fieldName = settings.getString("com.some.name");