Search code examples
javatestnginner-classesmaven-surefire-plugin

Unable to execute specific nested test class with Surefire


I have a slight problem with the Surefire plugin.

I have a nested test class hierarchy:

public class NestClassContainer {

    public static class TestClass1 {
        @Test
        public void testC1_m1() {

        }
    }


    public static class TestClass2 {
        @Test
        public void testC2_m1() {

        }
    }

}

My goal is to execute the methods in only one of the nested classes. Unfortunately I am unable to be specific with surefire (Example: only execute testC2_m1() ), even though this is easily possible in both TestNG and in IntelliJ.

I have tried:

mvn clean test -Dtest=NestClassContainer$TestClass2

or even:

mvn clean test -Dtest=com.adobe.campaign.tests.integro.phased.NestClassContainer$TestClass2

I have even tried removing the default excludes in the surefire configuration.

General information:

  • Testng 7.5
  • Maven 3.8.6
  • Surefire : 2.19.1, 2.22.1, 3.0.0-M1
  • java 1.8.0_341 JDK

Solution

  • I think this is just a shell escaping problem - are you on a *nix machine (as opposed to Windows)? If so, you need to escape the $, as it marks the start of a variable name.

    I've tried your example and it works fine:

    mvn test -Dtest=NestClassContainer\$TestClass2
    

    Alternatively, you can put the whole term in single quotes, which disables variable interpolation:

    mvn test -Dtest='NestClassContainer$TestClass2'
    

    Both methods show that only the specified nested test is run:

    ===== Invoked methods
        NestClassContainer$TestClass2.testC2_m1()[pri:0, instance:so73579735.NestClassContainer$TestClass2@6cc7b4de]
    =====
    [TestNG]
    [TestNG] ===============================================
    [TestNG]     Surefire test
    [TestNG]     Tests run: 1, Failures: 0, Skips: []
    [TestNG] ===============================================
    PASSED: so73579735.NestClassContainer$TestClass2.testC2_m1
    
    ===============================================
        Surefire test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================
    Surefire suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================
    

    (For this output, Surefire has to be configured to run testng in verbose mode)

      <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M1</version> 
                <configuration>
                    <properties>
                        <property>
                            <name>surefire.testng.verbose</name>
                            <value>10</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
      </build>
    

    On Windows it works with a plain $, because that character has no special meaning there.