测试数据提供程序时出现空指针异常



我遇到了一个奇怪的问题。让我解释一下:我从xml传递一组输入数据,然后使用JAXB解析xml。然后使用测试数据提供程序将这个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中删除新添加的块,它开始工作。请帮帮我!!

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

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

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

但是您是根据名称选择性地添加到数组中,因此尽管已经为3个对象完成了init,但数据仅对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) };
        }

相关内容

  • 没有找到相关文章

最新更新