我需要根据测试的参数以编程方式生成测试套件。
这是主要功能:
public class DataValidator {
public static void main (String args[]) {
TestState testState = new TestState();
//Create an instance on TestNG
TestNG testNg = new TestNG();
List<XmlSuite> suites = new ArrayList<XmlSuite>();
TestRunner testRunner = new TestRunner(testState);
XmlSuite testSuite = testRunner.generateSuite();
suites.add(testSuite);
testNg.setXmlSuites(suites);
testNg.run();
}
}
这是生成测试套件的测试运行程序
public class TestRunner {
private TestState testState;
public TestRunner(TestState testState) {
this.testState = testState;
}
public XmlSuite generateSuite( ) {
XmlSuite testSuite = new XmlSuite();
XmlTest xmlTest = new XmlTest();
xmlTest.setName("TestState");
List<XmlClass> xmlClasses = new ArrayList<XmlClass>();
xmlClasses.add(new XmlClass(JobCreated.class.getName()));
xmlTest.setXmlClasses(xmlClasses);
testSuite.addTest(xmlTest);
return testSuite;
}
这是工作创造的课程
public class JobCreated {
@Test
public void test1() {
System.out.println("passed test 1");
}
}
当我作为Java应用程序运行测试时,我得到以下NullPointerException:
Exception in thread "main" java.lang.NullPointerException
at org.testng.xml.XmlTest.getVerbose(XmlTest.java:249)
at org.testng.TestRunner.init(TestRunner.java:212)
at org.testng.TestRunner.<init>(TestRunner.java:167)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:663)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at data_validator_testng.DataValidator.main(DataValidator.java:39)
我无法理解导致此错误的原因。
答案 0 :(得分:0)
因为XmlTest和m_suite之间没有正确的链接为空
/**
* @return Returns the verbose.
*/
public int getVerbose() {
Integer result = m_verbose;
if (null == result || XmlSuite.DEFAULT_VERBOSE.equals(m_verbose)) {
result = m_suite.getVerbose();
}
通过在XmlTest的构造函数中添加testSuite
进行了纠正
XmlTest xmlTest = new XmlTest(testSuite);
此link对找到解决方案很有帮助