Reputation: 4763
I am using eclipse 2018-09 (4.9.0) with the testng plugin (version 6.14.0.201802161500). I created a maven project to learn testng from tutorials. I want to run a testng factory method in Eclipse but there is no "testng" option in the run menu. How do I make it run the factory ?
My code :
package com.learn.classes;
import org.testng.annotations.Test;
//run with testng is present for this.
public class SimpleTest {
@Test
public void simpleTest() {
System.out.println("Simple test Method");
}
}
package com.learn.factory;
import org.testng.annotations.Factory;
import com.learn.classes.SimpleTest;
//run with testng is NOT present for this.
public class SimpleTestFactory {
@Factory
public Object[] factoryMethod() {
return new Object [] {new SimpleTest(), new SimpleTest()};
}
}
SOLUTION : Create an xml suite for the above class or method and run that with testng.
Ex. myfactory.xml
<suite name="Factorty Methods" verbose="1">
<test name="My factory method">
<classes>
<class name="factory.SimpleTestFactory" />
<methods>
<include name="factoryMethod" />
</methods>
</classes>
</test>
</suite>
Upvotes: 2
Views: 412
Reputation: 14746
The reason why you see this behavior is because the TestNG eclipse plugin looks for any test methods (at-least one method annotated with @Test
annotation) in a class before enabling that contextual option via right click.
In the case of a factory (like how your sample code for SimpleTestFactory
looks like), there are no test methods. That is why it disables the Run As > TestNG test
option.
So one of the alternatives for this is to basically create a suite xml file, add a reference to SimpleTestFactory
and then run it via the suite file.
You could also file an issue on the eclipse TestNG plugin github page and ask if this can be fixed.
Ideally speaking a @Factory
annotated method could be regarded as a starting point as well. I don't know the feasibility of it though.
I do know that IntelliJ is able to do this.
Upvotes: 2