How to pass java code a parameter from maven for testing

You need not define anything for groups in testng xml or the pom, the support comes inbuilt. You can simply specify the groups on the cmd line http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

Hope it helps..

Edit 2:

Ok..so here's another option...Implement IMethodInterceptor

Define your custom property. Use -Dcustomproperty=groupthatneedstoberun in your command line call.

In the intercept call, scan through all methods ..something to the effect..

System.getProperty("customproperty");
for(IMethodInstance ins : methods) {
    if(ins.getMethod().getGroups()) contains group)
        Add to returnedVal;
    }
return returnedVal;

Add this to the listeners list in your xml.


This is the exact thing I was looking for my automation test and I got it working.

Command Line argument

mvn clean test -Denv.USER=UAT -Dgroups=Sniff

My Pom Xml

<?xml version="1.0" encoding="UTF-8"?>
<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>TestNg</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <systemPropertyVariables>
                        <environment>${env.USER}</environment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

TestNG test

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;


public class TestAuthentication {

    @Test (groups = { "Sniff", "Regression" })
    public void validAuthenticationTest(){
        System.out.println(" Sniff + Regression" + System.getProperty("environment"));
    }

    @Test (groups = { "Regression" },parameters = {"environment"})
    public void failedAuthenticationTest(String environment){
        System.out.println("Regression-"+environment);
    }

    @Parameters("environment")
    @Test (groups = { "Sniff"})
    public void newUserAuthenticationTest(String environment){
        System.out.println("Sniff-"+environment);
    }
}

The above works well. Additionally, if you need to use testng.xml, you can specify the suiteXmlFile like ...

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <systemPropertyVariables>
                    <environment>${env.USER}</environment>
                </systemPropertyVariables>
                <suiteXmlFiles> 
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

Also, I prefer using @Parameters instead of parameters in @Test() as the later is deprecated.