1

我遇到了一个奇怪的问题。让我解释一下:我从 xml 传递一组输入数据,然后使用 JAXB 解析 xml。然后使用 testng dataprovider 将此 java 对象传递给我的测试方法。以下是一些相关代码:Testdata xml:

<TestData>
<TestDetails>
    <testcasename>itemStatusTest</testcasename>
    <testcasedetails>App in SUPPRESSED Status</testcasedetails>
    <appid>28371</appid>
    <status>SUPPRESSED</status>
    <marketplace />
</TestDetails>
<TestDetails>
    <testcasename>itemStatusTest</testcasename>
    <testcasedetails>App in REVIEW Status</testcasedetails>
    <appid>22559</appid>
    <status>REVIEW</status>
    <marketplace />
</TestDetails>
</TestData>

返回对象的方法:

private static Object[][] generateTestData(String dataProvider,TestCaseName tcName) throws Exception {

    Object[][] obj = null;
     try {
        JAXBContext jaxbContext = JAXBContext.newInstance(TestData.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        TestData testData = (TestData) jaxbUnmarshaller
                .unmarshal(new FileInputStream(new File(dataProvider)
                        .getAbsoluteFile()));
        List<TestDetails> testcaseList = testData.getTestDetails();
        obj = new Object[testcaseList.size()][];
        for (int i = 0; i < testcaseList.size(); i++) {
            if (testcaseList
                    .get(i)
                    .getTestcasename()
                    .equalsIgnoreCase(tcName.testCaseName()))
                obj[i] = new Object[] { testcaseList.get(i) };
        }

    } catch (JAXBException e) {
        e.getMessage();
        return null;
    }
    return obj;
}

和我的数据提供者:

@DataProvider(parallel = true, name = "TestData")
public Object[][] TestData() {
    try {
        Object obj[][]= IngestionTestHelper
                .generateTestDataForItemStatus(dataProvider);
        Reporter.log("Size "+obj.length, true);
        return obj;
    } catch (Exception e) {
        Reporter.log(
                "Either XML input is in wrong format or XML is not parsed correctly",
                true);
        return null;
    }

}

到目前为止,一切都像魅力一样运作,我没有看到任何问题。

现在我正在为另一个测试用例编写另一个测试方法。为此,我在现有的 xml 中添加了以下内容,如下所示:

<TestDetails>
    <testcasename>itemWorkflowTest</testcasename>
    <testcasedetails>Validate workflow for iap</testcasedetails>
    <appid>26120</appid>
    <status />
    <marketplace />
</TestDetails>

现在,一旦我在现有的 xml 中添加了它,我现有的测试方法就不起作用了。运行时出现以下异常:

java.lang.NullPointerException
at org.testng.internal.Invoker.injectParameters(Invoker.java:1333)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1203)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

如果我在 xml 中删除新添加的块,它就会开始工作。请有人帮忙!!!

4

1 回答 1

0

好吧,根据代码,如果我理解正确的话:)

当您添加第三个项目时,名称不同,您已经使用元素总数的大小初始化了 Object 数组,

obj = new Object[testcaseList.size()][];

但是您正在根据名称有选择地添加到数组中,因此尽管已经对 3 个对象进行了初始化,但数据仅对 2 个对象可用 - 这可能会导致 NPE..

List<TestDetails> testcaseList = testData.getTestDetails();
        obj = new Object[testcaseList.size()][];
        for (int i = 0; i < testcaseList.size(); i++) {
            if (testcaseList
                    .get(i)
                    .getTestcasename()
                    .equalsIgnoreCase(tcName.testCaseName()))
                obj[i] = new Object[] { testcaseList.get(i) };
        }
于 2013-04-01T13:14:45.563 回答