How to pass java code a parameter from Maven for testing?
Last Updated :
27 Sep, 2024
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.
- Add the TestNG dependency: We’ll use This testing framework to write test cases.
- 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.
- In your project, navigate to the src/test/java directory.
- Create a package (e.g., com.example).
- 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
OutputConclusion
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.
Similar Reads
How to pass Parameter to testng.xml from Command Line with Maven? In test automation, we run tests across multiple web browsers. The TestNG with Maven allows us to pass the parameters through the command line dynamically; this enables us to control various test environments without editing the code again and again.Step to Pass Parameter to testng.xml from Command
3 min read
How to Pass Parameter from TestNG.xml File? Parameters in TestNG allow you to configure test methods flexibly and make them more maintainable and reusable. The parameters feature allows you to pass data directly from the testng.xml file to test methods. This increases reusability at the suite level without the need to hard code values into th
3 min read
How to Run a testng.xml File From Batch for a Maven Project? In a Maven project, testng.xml is a configuration file used by TestNG to define and organize which tests to run. To automate the process of running these tests, you can use a batch file. A batch file is a simple script that automates command-line tasks.Why Use a Batch File?A batch file allows you to
3 min read
How to call testng.xml file from pom.xml in Maven? Maven is a tool that is widely used for automation whenever developers work on Java projects. apart from the Java language, the Maven software supports other projects that are written in C#, Ruby, etc. The Maven project is controlled and hosted by the famous Apache software foundation.Table of Conte
4 min read
How to exclude TestNG Groups from Maven? To exclude specific TestNG groups from execution when running tests through Maven, you can configure the maven-surefire-plugin in the Maven pom.xml file. The Surefire plugin is responsible for running the unit tests during the build process, and it supports excluding certain groups from being execut
2 min read
How to Run Specific TestNG Suite with Maven from Command Line? When using Maven as your build automation tool and TestNG for Java-based testing, you may need to execute particular test suites instead of running every test. Running test subsets according to functionality, environment, or any other criteria can be helpful in this regard. You must set up your Mave
2 min read