Search code examples
javaeclipsejenkinsmaven-3junit4

Configuration for JUnit test execution on multiple environments


I have a Java project containing JUnit tests that needs to be run on different test environments (Dev, Staging, etc.) via Jenkins.

The solution that I currently have to build the project on the different environments and to to pass the url, username and the password to the test runner is to load specific property files for each environment, in the POM file. The property file will be set for each environment via the Maven build command:

mvn clean install -DappConfig=/src/test/resouces/integration.environment.properties

in pom.xml:

<plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <systemPropertyVariables>
                    <appConfig>${app.config}</appConfig>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>

In JUnit test runner class:

public class BoGeneralTest extends TestCase {

    protected WebDriver driver;
    protected BoHomePage boHomePage;
    protected static Properties systemProps;
    String url = systemProps.getProperty("Url");
    String username = systemProps.getProperty("Username");
    String password = systemProps.getProperty("Password");
    int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
    String regUsername = RandomStringUtils.randomAlphabetic(5);

    final static String appConfigPath = System.getProperty("appConfig");

    static {
        systemProps = new Properties();
        try {

            systemProps.load(new FileReader(new File(appConfigPath)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The problem with this configuration is that now the individual tests can't be run separately via Eclipse, because they expect to receive appConfig from maven and I'm getting NullPointerException.

Any suggestions are highly appreciated.


Solution

  • The precondition for running a single test is having a Run Configuration for each test case that will specify the default execution environment. Please note that this setting has to be done locally per test case.

    In the Eclipse Arguments tab/VM Arguments filed the VM parameter has to be specified:

    -DappConfig=src/test/resources/pp1.environment.properties
    

    It holds the path to the respective property file having the environment login details.

    There are five property files defined under src/test/resources source folder of the project:

    environment1.properties
    environment2.properties
    environment3.properties
    environment4.properties
    environment5.properties