testng dataprovider中的空指针异常

时间:2013-03-30 15:56:56

标签: xml-parsing jaxb testng objectdataprovider

我遇到了一个奇怪的问题。让我解释: 我从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;
    }

}

直到现在一切都像魅力一样,我没有看到任何问题。

现在我正在为另一个测试用例编写另一种测试方法。为此,我在我的exisitng 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中删除新添加的块,它就会开始工作。 请有人帮忙!!!

1 个答案:

答案 0 :(得分:0)

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

添加第三项时,名称不同, 您已使用元素总数

的大小初始化了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) };
        }