Open In App

How to pass java code a parameter from Maven for testing?

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Maven is one of the most commonly used tools in Java projects for build automation, allowing the developer to manage the dependency of their project and many other operations, like compiling codes or running tests. Sometimes, you may want to pass some parameters from Maven to the Java code, so you can use them in your testing. It might be very helpful when tests are run against different environments, with various configurations. Maven also facilitates the passing of parameters by using the command line, which can later be accessed from your Java code while executing the test.

Approach to pass Java code a parameter from Maven for testing

If you need to pass parameters from Maven to your Java test code, follow this step-by-step approach for setting up a Maven project, configuring it, and running your tests using the maven-surefire-plugin and TestNG.

Step 1: Create a Maven Project

The first step is to create a Maven project in your IDE, like Eclipse or IntelliJ.

Step 2: Add TestNG and Surefire Plugin to pom.xml

After the project creation, we have to update the pom.xml file to add dependencies and plugins to the project.

  1. Add the TestNG dependency: We’ll use This testing framework to write test cases.
  2. Add the maven-surefire-plugin: This plugin is responsible for running your tests. We’ll configure it to pass parameters.

Step 3: Create a Test Class

Now, create a test class where you’ll use the parameters passed from Maven. By default, Maven expects test classes to be placed under src/test/java.

  1. In your project, navigate to the src/test/java directory.
  2. Create a package (e.g., com.example).
  3. Inside that package, create a test class named EnvironmentTest.java.

Step 4: Run the Tests

Now that everything is set up, you can run the test by passing a custom parameter via the command line.

mvn test -Denvironment=name_of_variable

Code Example:

Java
package TestNG.SeleniumJava;

import org.testng.annotations.Test;

public class EnvironmentTest {

    @Test
    public void testEnvironmentProperty() {
        // Retrieve the environment parameter passed from Maven
        String environment = System.getProperty("environment");

        // Validate that the parameter is passed correctly
        if (environment != null) {
            System.out.println("Environment: " + environment);
        } else {
            System.out.println("No environment parameter passed.");
        }

        // Perform some test logic based on the environment
        if ("dev".equals(environment)) {
            System.out.println("Running tests in development environment.");
        } else if ("prod".equals(environment)) {
            System.out.println("Running tests in production environment.");
        } else {
            System.out.println("Running tests in default environment.");
        }
    }
}

POM.xml

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>TestNG</groupId>
  <artifactId>SeleniumJava</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SeleniumJava</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <systemPropertyVariables>
                        <environment>dev</environment>  <!-- Example of a parameter -->
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.7.1</version>
    <scope>test</scope>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Running test using terminal

Open terminal and add the following line:

mvn test -Denvironment=prod

Output

output-
Output
output
Output

Conclusion

Passing parameters from Maven to the Java code for testing is simple and can be defined through properties in the pom.xml file or even through the command line. Using System.getProperty() will enable you to retrieve these parameters and apply them within your test cases, hence tests get run for different configurations or environments, and this technique comes really handy in parameterized testing, and thereby, the degree of flexibility to manage diverse testing scenarios increases.


Next Article

Similar Reads