测试工厂在使用前缀命名时抛出NPE

时间:2016-07-21 11:59:38

标签: java maven testng

TestNG 6.8.8, Ubuntu 16.04 LTS, Apache Maven 3.3.9

我有以下TestCase和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) };
    }

}

当我尝试在cmd中运行mvn test时,我遇到以下异常:

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)

但是在将MyTestSuite重命名为TestSuiteSomeSuffix后,mvn test执行得很好。我试图将其重命名为TestSomeSuffix,但它也能正常工作。

TesSomeSuffix会发出我在上面发布的例外情况。这是什么?为什么它取决于命名?这个例外有一个非常奇怪的消息BTW。

1 个答案:

答案 0 :(得分:1)

来自the surefire documentation

  

默认包含

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

这解释了为什么您看到TestCase / TestSuiteSomeSuffix / TestSomeSuffixMyTestSuite之间存在一些差异。

在您的示例中,您不希望将TestCase包含在surefire中,而只包含MyTestSuite

我看到两个选项:

  1. 根据默认的surefire配置重命名文件,或
  2. 配置surefire。