dataprovider iterator type case exception

119 views
Skip to first unread message

new TestNG user

unread,
Nov 17, 2009, 6:40:21 PM11/17/09
to testng-users
Hi,

I am trying to use the TestNG data provider to iterate data from a
data file. But I always get the following type case exception. Can
someone help in this issue? I am using TestNG 5.8/5.9.

[testng] Exception in thread "main" java.lang.ClassCastException:
java.lang.String
[testng] at org.testng.internal.Invoker.invokeTestMethods
(Invoker.java:952)
[testng] at
org.testng.internal.TestMethodWorker.invokeTestMethods
(TestMethodWorker.java:126)
[testng] at org.testng.internal.TestMethodWorker.run
(TestMethodWorker.java:110)
[testng] at org.testng.TestRunner.runWorkers(TestRunner.java:
721)
[testng] at org.testng.TestRunner.privateRun(TestRunner.java:
591)
[testng] at org.testng.TestRunner.run(TestRunner.java:485)
[testng] at org.testng.SuiteRunner.runTest(SuiteRunner.java:
332)
[testng] at org.testng.SuiteRunner.runSequentially
(SuiteRunner.java:327)
[testng] at org.testng.SuiteRunner.privateRun(SuiteRunner.java:
299)
[testng] at org.testng.SuiteRunner.run(SuiteRunner.java:204)
[testng] at org.testng.TestNG.createAndRunSuiteRunners
(TestNG.java:864)
[testng] at org.testng.TestNG.runSuitesLocally(TestNG.java:830)
[testng] at org.testng.TestNG.run(TestNG.java:748)
[testng] at org.testng.TestNG.privateMain(TestNG.java:901)
[testng] at org.testng.TestNG.main(TestNG.java:874)


Here is my code:

DataIterator.java:
================

public class DataIterator implements Iterator
{
private int nextIndex = 0;
private String testDataFilePath = null;
private String[] testData = null;

private boolean InitializeData()
{
// Test data already initialized
if(null != testData)
{
return true;
}

//
if(null == testDataFilePath)
{
return false;
}

// Try load the test data from the file
try
{
testData = FileUtils.loadTextFromFile(testDataFilePath).split
("\n");

return true;
}
catch (Exception e)
{
e.printStackTrace();
}

return false;
}

public void SetTestDataPath(String filePath)
{
this.testDataFilePath = filePath;
}

public boolean hasNext()
{
InitializeDataFromFile();

if(null != testData && nextIndex < testData.length)
{
return true;
}

return false;
}

public Object next()
{
InitializeDataFromFile();

if(null != testData && nextIndex < testData.length)
{
return testData[nextIndex++];
}

return null;
}

public void remove() {}
}

SampleTest.java
==================

public class SampleTest
{
protected String testDataPath = null;

@DataProvider
public Iterator defaultDataProvider()
{
DataIterator dataIterator = new DataIterator();
dataIterator.SetTestDataPath(testDataPath);

return dataIterator;
}

@BeforeTest
public void init()
{
testDataPath = "./TestData/test.csv";
}

@Test (dataProvider = "defaultDataProvider")
public void Test1(String testData)
throws SkipException, Exception
{
System.out.println(testData);
}
}

Cédric Beust ♔

unread,
Nov 17, 2009, 6:45:15 PM11/17/09
to testng...@googlegroups.com
Each object that you return from next() should be an Object[], one element for each parameter of your test method (so in your case, an array of one String), are you sure that's what you're doing?

--
Cédric




--

You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=.





new TestNG user

unread,
Nov 17, 2009, 7:07:07 PM11/17/09
to testng-users
thanks Ceric! Yes, that fixes the exception in my code.

On Nov 17, 3:45 pm, Cédric Beust ♔ <cbe...@google.com> wrote:
> Each object that you return from next() should be an Object[], one element
> for each parameter of your test method (so in your case, an array of one
> String), are you sure that's what you're doing?
>
> --

> ***Cédric
> *

Reply all
Reply to author
Forward
0 new messages