0

I do have Configuration of Maven + TestNG + Selenium + Java

I wants to run specific Java class from POM.XMl file,

Tried:

     <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>demo.java</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

There is common solution provided as far I referred, But it did not worked. It didn't run anything and Build was success,

Please note, As using TestNG there is not public static void main(String args[]) in any of Java class.

It would be great to have explanation on <mainClass>demo.java</mainClass> part, With reference as TESTNG class.

So far I have tried :

From Maven, how do I run a class that lives under src/test/java?

How to execute a java class file from maven command line

How do I execute a program using Maven?

Maven Run Project

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51

3 Answers3

0

exec-maven-plugin requires public static void main(String[] args) method.

In order to run TestNG class from exec-maven-plugin you would have to run TestNG in main method. Try this solution:

public static void main(String[] args) {
    TestNG testSuite = new TestNG();
    testSuite.setTestClasses(new Class[] { MyClass.class }); //it would be probably name of the class in which `main` method is located
    testSuite.addListener(new MyListener()); //optional
    testSuite.setDefaultSuiteName("Test Suite");
    testSuite.setDefaultTestName("Test Name");
    testSuite.setOutputDirectory("/my/output/dir");
    testSuite.run();
}
Fenio
  • 3,528
  • 1
  • 13
  • 27
0

Since use are using Maven+ testng. In pom.xml u can use maven-surefire-plugin for running specific TestCase mentioned in testng.xml.Variable TestCaseNames in pom.xml (multiple testcases with, seperated). U can mentioned your TestCase in Testng.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>                     
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <properties>
                        <property>
                            <name>testnames</name>
                            <value>${TestCaseNames}</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>

In TestNg:

<test name="TestCase1">
        <classes>
            <class name="com.company.test">
                <methods>
                    <include name="testCase1" />
                </methods>
            </class>
        </classes>
    </test>
0

I remember, Once I was gone through this and it worked.

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <includes>
                    <include>demo.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>

Reference : http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51