简体   繁体   中英

Get NPE when loading correct properties file with Maven profile

I created two Maven profile. One is for dev, one is for qa.

<profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
           <properties>
               <cofiguration.path>src/test/resources</cofiguration.path>
           </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.18.1</version>
                        <configuration>
                            <systemPropertyVariables>
                                <appEnv>dev</appEnv>
                                <userFile>application.properties</userFile>
                                <Selenium_Broswer>chrome</Selenium_Broswer>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>qa</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.18.1</version>
                        <configuration>
                            <systemPropertyVariables>
                                <appEnv>qa</appEnv>
                                <userFile>application.properties</userFile>
                                <Selenium_Broswer>firefox</Selenium_Broswer>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

Also have a class to read the properties

public class PropertyFile {

    private static Properties prop;

    private static void getPropertyFile() {
        String appFilePath = System.getProperty("user.dir") + "/src/test/resources/" + System.getProperty("appEnv") + ".properties";
        try (InputStream in = new FileInputStream(appFilePath)) {
            prop = new Properties();
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String propertyName) {
        if (prop == null) {
            getPropertyFile();
        }
        return prop.getProperty(propertyName);
    }
}

Basically, what I want to do is when I run mvn test -Pdev. It will get the variables in the maven profile "appEnv"

For example, appEnv is dev when mvn test -Pdev. In the PropertyFile class, it will get the appEnv and find correct properties file(I have dev.properties and qa.properties under resources folder and there are some base url and other properties in the files)

But now , it returns NPE when I call PropertyFile.getProperty("baseUrl") . What is the problem? How should I change the code and maven profile?

Just fix your NPE:

private static Properties prop=new Properties();

and

if (prop.isEmpty()) {
    getPropertyFile();
}

Pls using Resources.getResource(path) to get resource file. And if is Spring project, it is a good way to add different profile at the application file then add a property bean to load the value.

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