Hello,
I'm a new TestNG user.
In the following situation
public
class
SimpleTest
{
private String name;
public SimpleTest(String name) {
}
@Test
public
void
test() {
System.out.println(
"Simple Test Method. Instance:" + name
);
}
}
public
class
SimpleTestFactory
{
@Factory
public
Object[] factoryMethod() {
return
new
Object[] {
new
SimpleTest("foo"),
new
SimpleTest("bar") };
}
}
When I right click SimpleTestFactory and do "Run as TestNG Test", TestNG uses the @Factory method to instantiate SimpleTest twice.
So far so good. If I right click the package and do "Run as TestNG Test", behavior is the same as right clicking SimpleTestFactory.
Now, I use Eclipse to duplicate SimpleTest into SimpleTest2 and right click the package again, and do "Run as TestNG Test". This time, I get "Default test name" in the output!
Somehow, TestNG decides when a test class as constructor that takes a single String parameter, it must be the test name... That's curious.
public
class
SimpleTest2
{
private String name;
public SimpleTest2(String name) {
}
@Test
public
void
test() {
System.out.println(
"Simple Test Method. Instance:" + name
);
}
}
Now, if I change SimpleTest2 so that the sole constructor takes an int parameter, right clicking the package and doing "Run as TestNG Test" causes an exception:
public
class
SimpleTest2
{
private String name;
public SimpleTest2(int i) {
}
@Test
public
void
simpleTest() {
System.out.println(
"Simple Test Method. Instance:" + name
);
}
}
org.testng.TestNGException:
Can't invoke public test.SimpleTest2.test(): either make it static or add a no-args constructor to your class
So I thought, that's fine, I'm going to add another factory method
public
class
SimpleTestFactory
{
@Factory
public
Object[] factoryMethod() {
return
new
Object[] {
new
SimpleTest("foo"),
new
SimpleTest("bar") };
}
@Factory
public
Object[] factoryMethod2() {
return
new
Object[] {
new
SimpleTest2(1),
new
SimpleTest2(2) };
}
}
Except the second method annotated with "@Factory" is ignored by TestNG.
I had to create a SimpleTestFactory2 class
public
class
SimpleTestFactory2
{
@Factory
public
Object[] factoryMethod() {
return
new
Object[] {
new
SimpleTest2(1),
new
SimpleTest2(2) };
}
}
All in all:
1. is this normal TestNG instantiate a test class that takes a single String as its constructor parameter with "Default test name" ?
2. is this normal one can't have 2 factory methods in the same factory class?
3. can you confirmed TestNG won't try to instantiate a test class as a standalone class when it's meant to be used (and is effectively used) by method annotated with @Factory ?
Thank you