简体   繁体   中英

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

I use wicket in my webapplication. I save the Strings in some .properties files as follows:

foo.properties

page.label=dummy

In the html-file, I can acces the String page.label as follows:

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. 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. See the javadoc for the StringResourceLoader .

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:

com.some.name=someValueHere

Finally you can access the property values this way:

private String fieldName = settings.getString("com.some.name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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