简体   繁体   English

从测试运行时,getResourceAsStream(“file”)在哪里搜索?

[英]Where is getResourceAsStream(“file”) searching when running from a test?

I'm struggling to create a test to verify a ServletListener that loads a properties file. 我正在努力创建一个测试来验证加载属性文件的ServletListener I've tested when running the application that it works fine and it finds the file in the classpath . 我在运行应用程序时测试它运行正常,并在classpath找到该文件。 But I don't know how to create a test for it. 但我不知道如何为它创建测试。 My idea is to create a temp file with a test property and then verify the property is put into the System properties . 我的想法是创建一个带有test属性的临时文件,然后验证该属性是否已放入System properties But I always fail to create the file in the right place. 但我总是无法在正确的位置创建文件。

I've tried creating the file in /target/test-classes or directly in the root of the application but it never finds it. 我尝试在/target/test-classes创建文件,或者直接在应用程序的根目录中创建文件,但它从未找到它。 Any idea? 任何的想法?

This is the code I'm trying to test: 这是我试图测试的代码:

public class PropertyReadingListener implements ServletContextListener {

    public static final String PROFILES_PROPERTIES = "profiles.properties";

    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        Properties propsFromFile = new Properties();
        try {
            propsFromFile.load(getClass().getResourceAsStream(PROFILES_PROPERTIES));
        } catch (final Exception e) {
            log.warn("Unable to find {}, using default profile", PROFILES_PROPERTIES);
        }
        propsFromFile.stringPropertyNames().stream()
            .filter(prop -> System.getProperty(prop) == null)
            .forEach(prop -> System.setProperty(prop, propsFromFile.getProperty(prop)));
    }
}

Thanks. 谢谢。

Where is getResourceAsStream("file") searching when running from a test? 从测试运行时,getResourceAsStream(“file”)在哪里搜索?

Assuming that you are talking about JUnit .... 假设你在谈论JUnit ....

Unless you have done something funky, your unit tests will be loaded by the default classloader, and that means that the normal JVM classpath with be searched. 除非你做了一些时髦的事情,否则你的单元测试将由默认的类加载器加载,这意味着要搜索正常的JVM类路径。

(Junit 4 allows you to use a different classloader: see https://stackoverflow.com/a/9192126/139985 ) (Junit 4允许您使用不同的类加载器:请参阅https://stackoverflow.com/a/9192126/139985


But I always fail to create the file in the right place. 但我总是无法在正确的位置创建文件。

It seems that your real problem is understanding how the Java classpath and classpath searching works. 您的真正问题似乎是了解Java类路径和类路径搜索的工作原理。 If you understand that (and you know what JUnit runner's actual classpath is) then it should be obvious where to put the properties file so that the classloader can find it. 如果您了解(并且您知道JUnit运行器的实际类路径是什么)那么应该明确放置属性文件的位置,以便类加载器可以找到它。

Assuming that you are using maven, put your properties file here: 假设您正在使用maven,请将您的属性文件放在此处:

src/test/resources/foo.properties

The maven resources plugin will place the (possibly filtered) copy of the file in maven资源插件将放置文件的(可能已过滤的)副本

target/test-classes/foo.properties

The test-classes directory is on the classpath, so you reference the file like this (note the slash in front of the file name): test-classes目录位于类路径中,因此您可以像这样引用文件(请注意文件名前面的斜杠):

... getResourceAsStream("/foo.properties");

See Different ways of loading a file as an InputStream 请参阅将文件加载为InputStream的不同方法

Basically when you do a getClass().getResourceAsStream it looks in the package of that class for the file.. so if your PropertyReadingListener is in com.company.listeners.PropertyReadingListener then it will look in com/company/listeners for the property file. 基本上当你执行getClass()。getResourceAsStream时,它会在该类的包中查找该文件..所以如果你的PropertyReadingListener在com.company.listeners.PropertyReadingListener中,那么它将在com / company / listeners中查找属性文件。

For testability, I would pass in an InputStream into the listener that way the test can create the input stream in a convienent way and the actual user of the class in code can pass in the InputStream returned from getResourceAsStream 为了可测试性,我将一个InputStream传递给监听器,这样测试可以以一种方式创建输入流,并且代码中类的实际用户可以传入从getResourceAsStream返回的InputStream。

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

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