gpt4 book ai didi

java - 当使用前缀命名时,测试工厂会抛出 NPE

转载 作者:行者123 更新时间:2023-12-01 09:40:25 25 4
gpt4 key购买 nike

TestNG 6.8.8, Ubuntu 16.04 LTS, Apache Maven 3.3.9

我有以下测试用例和测试套件:

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 发出了我上面发布的异常。这是什么?为什么它取决于命名?顺便说一句,异常有一个非常奇怪的消息。

最佳答案

来自the surefire documentation :

the default includes will be

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

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

在您的示例中,您不想将 TestCase 包含在 Surefire 中,而只想将 MyTestSuite 包含在内。

我看到 2 个选项:

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

关于java - 当使用前缀命名时,测试工厂会抛出 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com