0
votes

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?

1
1. The src direcgtory won't be there at runtime, so you have to look for your file, probably in a resource directory that will be present at runtime. 2. Resources are not files. You should be using getResourceAsStream() here, and doing a bit better with your null-checking. - user207421

1 Answers

0
votes

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.