简体   繁体   English

如何在junit-tests中读取.properties文件中的字符串?

[英]How to read string from .properties file in junit-tests?

I use wicket in my webapplication. 我在webapplication中使用wicket。 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文件中,我可以访问String page.label ,如下所示:

index.html 的index.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. 现在我为我的应用程序编写了一些junit测试用例,并希望访问属性文件中保存的字符串。 My Question is, how to read the String from the properties file? 我的问题是,如何从属性文件中读取字符串?

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;
    }
}

Wicket has its own way of localizing the resource, taking into account the component tree. Wicket有自己的方法来本地化资源,同时考虑组件树。 See the javadoc for the StringResourceLoader . 请参阅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(), "")

使用Apache Commons Configuration是一个不错的选择!

你可以使用load然后get("page.label")

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM