Jorn Vernee
2018-11-13 19:02:36 UTC
Hi,
I was testing using a testng test akin to the following:
import org.testng.TestNG;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class Main {
public static class MyTest {
private final int i;
public MyTest(int i) {
this.i = i;
}
@Test
public void testDownCall() {
System.out.println("test: " + i);
}
}
@Factory
public Object[] getTests() throws Throwable {
return generateTests();
}
private Object[] generateTests() {
return new Object[] {}; // erroneously returning empty array
}
public static void main(String[] args) throws Throwable {
TestNG t = new TestNG();
t.setTestClasses(new Class<?>[] { Main.class });
t.run();
}
}
Running this with the 7.0.0-beta1 version of TestNG I get the following
error:
Exception in thread "main" org.testng.TestNGException:
An error occurred while instantiating class test.Main$MyTest. Check to make
sure it can be instantiated
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:388)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:188)
at org.testng.TestClass.getInstances(TestClass.java:94)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:79)
at org.testng.TestClass.init(TestClass.java:71)
at org.testng.TestClass.<init>(TestClass.java:36)
at org.testng.TestRunner.initMethods(TestRunner.java:461)
at org.testng.TestRunner.init(TestRunner.java:340)
at org.testng.TestRunner.init(TestRunner.java:293)
at org.testng.TestRunner.<init>(TestRunner.java:178)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(
SuiteRunner.java:603)
at org.testng.SuiteRunner.init(SuiteRunner.java:194)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:125)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1230)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1209)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1058)
at org.testng.TestNG.runSuites(TestNG.java:997)
at org.testng.TestNG.run(TestNG.java:965)
at test.Main.main(Main.java:32)
I spent a few days puzzled with this, since the test was working fine
before. But it turned out that the method generating the test classes
(which in reality is quite a bit more complex) was suddenly returning an
empty array.
Maybe a warning or error should be emitted when an @Factory method returns
an empty array? Instead of silently falling back on trying to construct
test instances automatically.
I was testing using a testng test akin to the following:
import org.testng.TestNG;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class Main {
public static class MyTest {
private final int i;
public MyTest(int i) {
this.i = i;
}
@Test
public void testDownCall() {
System.out.println("test: " + i);
}
}
@Factory
public Object[] getTests() throws Throwable {
return generateTests();
}
private Object[] generateTests() {
return new Object[] {}; // erroneously returning empty array
}
public static void main(String[] args) throws Throwable {
TestNG t = new TestNG();
t.setTestClasses(new Class<?>[] { Main.class });
t.run();
}
}
Running this with the 7.0.0-beta1 version of TestNG I get the following
error:
Exception in thread "main" org.testng.TestNGException:
An error occurred while instantiating class test.Main$MyTest. Check to make
sure it can be instantiated
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:388)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:188)
at org.testng.TestClass.getInstances(TestClass.java:94)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:79)
at org.testng.TestClass.init(TestClass.java:71)
at org.testng.TestClass.<init>(TestClass.java:36)
at org.testng.TestRunner.initMethods(TestRunner.java:461)
at org.testng.TestRunner.init(TestRunner.java:340)
at org.testng.TestRunner.init(TestRunner.java:293)
at org.testng.TestRunner.<init>(TestRunner.java:178)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(
SuiteRunner.java:603)
at org.testng.SuiteRunner.init(SuiteRunner.java:194)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:125)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1230)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1209)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1058)
at org.testng.TestNG.runSuites(TestNG.java:997)
at org.testng.TestNG.run(TestNG.java:965)
at test.Main.main(Main.java:32)
I spent a few days puzzled with this, since the test was working fine
before. But it turned out that the method generating the test classes
(which in reality is quite a bit more complex) was suddenly returning an
empty array.
Maybe a warning or error should be emitted when an @Factory method returns
an empty array? Instead of silently falling back on trying to construct
test instances automatically.
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+***@googlegroups.com.
To post to this group, send email to testng-***@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+***@googlegroups.com.
To post to this group, send email to testng-***@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.