I have some TestNG
based LeanFT
test cases and try to generate jar
file. I use IntelliJ IDEA
to set artifact's details under File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies
. I select classname, but get error, that is not acceptable.
UPDATE 2018.05.03. I created main method in a new class, but got same error message.
import org.testng.TestNG;
import org.testng.xml.Parser;
import org.testng.xml.XmlSuite;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
public class LeanFTest {
public void main() throws IOException, SAXException, ParserConfigurationException {
TestNG testNG = new TestNG();
String xmlFileName = "testng.xml";
List<XmlSuite> suite = (List<XmlSuite>)(new Parser(xmlFileName).parse());
testNG.setXmlSuites(suite);
testNG.run();
}
}
It seems you have some kind of combination between test project (using LeanFT TestNG template), java app, who-knows-what-else app.
If you have a main
method and still want to trigger TestNG tests, you need to use the TestNG class. For example
TestNG testNG = new TestNG();
testNG.setTestClasses(WebTestFactory.class);
testNG.run();
You can read more about this approach in the official docs or in this SO thread
If you don't have a main
class, you should create one. (how else could a .jar
file know what is the entry point?).
All in all, this error indicates there's a conflict between project type and project structure (content)
As per your recent comment: Could you please show me example/pattern, where to put main() method?
LeanFTest
Create main
method.
Whatever you do in the main method will drive your whole app. In your specific case (executing TestNG tests) you will need to do the followings, in your main method:
Create a test ng instance (TestNG testNG = new TestNG();
)
Use this instance to prepare the test suite
Pointing towards the SO thread above (again), it would mean something like:
String xmlFileName = "testng.xml";
List<XmlSuite> suite = (List <XmlSuite>)(new Parser(xmlFileName).parse());
testNG.setXmlSuites(suite);
Run the suite
testNG.run();
After that, when you create your artifact, you point to the class that has the main method and double clicking the .jar
(or executing it from command line) should start the test suite.