1

I am running in a strange problem. Let me explain: I am passing set of input data from xml and then using JAXB to parse xml. This java object is then passed to my test method using testng dataprovider. Here are some related code: 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>

Method which returns object:

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;
}

and my dataprovider:

@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;
    }

}

Till now everything works like a charm and I am not seeing any issue.

Now i am writing another test method for another test-case. For that I have added following in my exisitng xml like this:

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

Now once i have added this in my existing xml my existing test method is not working. When running I am getting following exception:

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)

If i remove the newly added block in xml it starts working. Please someone help!!!

Pratik
  • 952
  • 2
  • 16
  • 31

1 Answers1

0

Well, based on the code, and if I understood correctly :)

When you add the third item the name is different, You have initialized the Object array with the size of the total number of elements,

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

But you are adding to the array selectively based on name, so though the init has been done for 3 objects, the data is available only for 2 - this may be causing the 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) };
        }
niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Thanks niharika_neo. Yes after debugging i found out that reason for NPE is null object. I am currently checking if object == null then dont use it in test-case but its kind of hack and not a clean solution.Is there a better way out? – Pratik Apr 06 '13 at 04:23