user3663882
user3663882

Reputation: 7357

Test factory throws NPE when named with prefix

TestNG 6.8.8, Ubuntu 16.04 LTS, Apache Maven 3.3.9

I have the following TestCase and TestSuite:

public class TestCase implements ITest{
    private final int i;

    public TestCase(int i) {
        this.i = i;
    }

    public String getTestName() {
        return "name";
    }

    @Test
    public void test(){
        System.out.println(i);
    }
}

public class MyTestSuite {

    @Factory
    public Object[] testCases(){
        return new Object[]{ new TestCase(1), new TestCase(2) };
    }

}

When I try to run mvn test in cmd I got the following exception:

Running com.test.b.TestCase
Configuring TestNG with: TestNG652Configurator
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.166 sec <<< FAILURE! - in com.test.b.TestCase
test(com.test.b.TestCase)  Time elapsed: 0.008 sec  <<< FAILURE!
org.testng.TestNGException: 
Can't invoke public void com.test.b.TestCase.test(): either make it static or add a no-args constructor to your class
        at org.testng.internal.Utils.checkInstanceOrStatic(Utils.java:795)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:40)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
        at org.testng.SuiteRunner.run(SuiteRunner.java:254)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:115)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSingleClass(TestNGDirectoryTestSuite.java:128)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:112)
        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:112)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

But after renaming MyTestSuite to for instance, TestSuiteSomeSuffix, mvn test executes just fine. I tried to rename it to TestSomeSuffix and it worked as well.

But TesSomeSuffix emits the exception I posted above. What is this? Why does it depend on naming? The exception has a very weird message BTW.

Upvotes: 0

Views: 43

Answers (1)

juherr
juherr

Reputation: 5740

From the surefire documentation:

the default includes will be

<includes>
   <include>**/Test*.java</include>
   <include>**/*Test.java</include>
   <include>**/*TestCase.java</include>
</includes>

That explains why you see some difference between TestCase/TestSuiteSomeSuffix/TestSomeSuffix and MyTestSuite.

In your sample, you don't want to have TestCase included in surefire but only MyTestSuite.

I see 2 options :

  1. rename files according default surefire configuration, or
  2. configure surefire.

Upvotes: 1

Related Questions