Exception seen when using Factory annotation on a Constructor

1,396 views
Skip to first unread message

John

unread,
May 28, 2012, 11:47:16 AM5/28/12
to testng...@googlegroups.com
Hi,

I am trying to use the factory annotation on the constructor and trying to instantiate the same class (in which the factory annotation exists) multiple times by reading data from a DataProvider. The data is correctly read from the file that contains the test data, but then when its time to instantiate the class, it throws and exception. I debugged the class and found out that its raising this in the FactoryMethod.class file line 86. I have pasted the source code below.

          Object instance = com.getConstructor().newInstance(parameters);
          result.add(instance);

The exception gets raised on the first line itself.

There is only 1 class, I am posting the class below, the FBTestDriver Data Provider reads the required data from a file and its an object [][], so its a two dimensional array. Its not very clear if the same class can be instantiated using the factory annotation or not, so I could be doing something not possible technically, so please correct me if I am doing something wrong here.

public class FBTest extends DataSetup{
@Factory(dataProviderClass = DataProviders.class, dataProvider = "FBTestDriver")
public FBTest(String selectionextid, String classid, String typeid, float decimalprice) throws Throwable{
this.SelectionExtId = selectionextid;
this.ClassId = classid;
this.TypeId = typeid;
this.DecimalPrice = decimalprice;

//Selenium code;
}
  @Test
  public void CheckWinPrice() {
 try {
 //Selenium code using the above parameters
 } catch (Throwable e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }  
  }
  
  @Test
  public void CheckWinPercent() {
 try {
 //Selenium code using the above parameters

 } catch (Throwable e) {
 //TODO Auto-generated catch block
 e.printStackTrace();
 }  
  }
}

Thank you,
John

Cédric Beust ♔

unread,
May 28, 2012, 12:25:23 PM5/28/12
to testng...@googlegroups.com
You didn't include your data provider nor thee xception you're seeing.

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/VNfBevuKtEwJ.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

John

unread,
May 29, 2012, 3:35:22 AM5/29/12
to testng...@googlegroups.com
Hi Cédric,

The data provider is a method that reads data from a CSV file and passes it back as a two dimensional array... I am calling the factory class from the testNG.xml file.

Here's the code for DataProvider:

@DataProvider(name = "FBTestDriver")
public static Object[][] DataStorage() throws FileNotFoundException {
String [][] Values = null;
String Datadir = System.getProperty("user.dir") + InitApp.config.getValue("DATA_PATH");

                String ClassName = "FBTest";
ReadCSV obj = new ReadCSV(Datadir + ClassName + ".csv").skipHeader();
try {
Values = obj.ReadIncludedTestData();
} catch (IOException e) {
System.out.println("File " + ClassName + ".csv Not Found!!! " + e);
}
System.out.println("No of Rows passed: " + Values.length);//Debug line
return (Object[][]) Values;
}

And the exception I see is below, its very generic:

org.testng.TestNGException: 
The factory method class tests.FBTest.tests.FBTest() threw an exception
at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:93)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:140)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:139)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:269)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1260)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1247)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1101)
at org.testng.TestNG.run(TestNG.java:1022)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:86)

The TestNG XML file contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Tests">
  <test name="FF Test" parallel="false" preserve-order="true">
    <classes>
      <class name="tests.FBTest"/>
    </classes>
  </test>
</suite>

Please help...

Thank you,
John

-- 
Cédric




To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

Cédric Beust ♔

unread,
May 29, 2012, 12:59:00 PM5/29/12
to testng...@googlegroups.com
You're still not providing the important part, which is what your data provider is returning. Please provide a simple class that I can compile on my end. You might even find the problem yourself while creating this small test case.

Also, your local variables should start with a lowercase letter.

-- 
Cédric




To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/He06MmTTHK0J.

To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

John

unread,
May 30, 2012, 3:34:26 AM5/30/12
to testng...@googlegroups.com
This is what the Data Provider returns to the Factory Constructor:

1 223 1943 2649 6227 75201 2012-05-28 15:00:00 0
1 223 1943 2649 6227 75203 2012-05-28 15:00:00 0
1 223 1943 2650 6228 75214 2012-05-28 15:30:00 0

Thank you,
John

Cédric Beust ♔

unread,
May 30, 2012, 3:38:04 AM5/30/12
to testng...@googlegroups.com
I give up.

-- 
Cédric




To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/sUNLcPOPAm8J.

To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

John

unread,
May 30, 2012, 3:44:58 AM5/30/12
to testng...@googlegroups.com
Sorry Cedric, I missed the 2nd bit of your mail. I will send you a sample class in a few hours time, right now a little busy. 

Thank for your help till now, very much appreciated.

Thank you,
John

Tsiakos, Panagiotis

unread,
May 30, 2012, 3:53:21 AM5/30/12
to testng...@googlegroups.com

Hi all, some coleagues while running sone GUI automation tests from Eclipse they keep getting the following exception during execution although the final resuts

are passed. Here is a screenshot of their classpath. They have the latest TestNG plugin for Eclipse

 

I saw in the community that this is a known issue if you use IDEA.

Have you any idea what maybe be the cause of this exception in my case?

 

Also a second question just for info

I saw that the selenium-server-standalone.jar includes the testNG libraries.

So in the case that the testng-6.5.1.jar is also in the classpath which testNG libraries are used

during the test execution?

 

Than you in advance

 

Panagiotis

 

Panagiotis Tsiakos

Siemens Enterprise Communications S.A.

Enterprise Product Development
15 Andrea Metaxa str., Room 1.14

GR 145 64, Nea Kifisia
Athens, Greece
Tel: +30 210 8189609

Fax: +30 210 8189761
mailto: panagiotis.tsiakos@siemens-enterprise.com

 

Krishnan Mahadevan

unread,
May 30, 2012, 3:57:28 AM5/30/12
to testng...@googlegroups.com
Panagiotis 

Please DONOT hijack threads and cross post. Please start a new thread for whatever problem you are facing, because your post and the issue being discussed in this thread seem to be UNRELATED.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
Outlook.jpg

Krishnan Mahadevan

unread,
May 30, 2012, 4:04:34 AM5/30/12
to testng...@googlegroups.com
John,

Here's a simple program which will recreate the exact issue that you are facing :

package raw.code;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class DataProviderErrorTest {
private int a;
private int b;

public DataProviderErrorTest(int a, int b) {
this.a = a;
this.b = b;
}

@Test
public void testMethod() {
System.out.println("Value = " + this.a);
System.out.println("Value = " + this.b);
}

@DataProvider(name = "MyDP")
public static Object[][] createData() {
return new Object[][] { { "1", 2 }, { "3", 4 } };
}

@Factory(dataProvider = "MyDP")
public static Object[] createTestInstances(int a, int b) {
return new Object[] { new DataProviderErrorTest(a, b) };
}
}


Here I am operating with a @Factory annotation, and this factory relies on a @DataProvider for figuring out how many iterations that need to be carried out.

If you observe closely, you would notice that my data provider is giving me 1 string and 1 int PER ITERATION.
But if you see, my @Factory annotation is capable of receiving 2 ints as params.

net result :

Caused by: java.lang.IllegalArgumentException: argument type mismatch

Can you please cross check if your data provider is returning data in the exact same type as being expected by your factory annotation. ?

because from what you have shared it doesnt look so :

Here I am seeing that your factory is expecting the data provider to provide it with 3 strings and 1 float.
@Factory(dataProviderClass = DataProviders.class, dataProvider = "FBTestDriver")
public FBTest(String selectionextid, String classid, String typeid, float decimalprice) throws Throwable{

But in your data provider, I am seeing that you are basically trying to return back an array of Strings (String [][] Values = null;)

Its not going to work. Moreover we still dont know what exactly happens when you invoke obj.ReadIncludedTestData(). Does this method return you back a double dimension array of Strings ?
If thats the case, then you might have to change your @Factory's last parameter viz "decimalprice" to String data type and internally use Float.parseFloat(decimalprice) to convert it from a String to Float. (Assuming you absolutely want it to be a float only)


@DataProvider(name = "FBTestDriver")
public static Object[][] DataStorage() throws FileNotFoundException {
String [][] Values = null;
Values = obj.ReadIncludedTestData();



Hope that helps in some way.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/oEaAlfc5dHcJ.

To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

John

unread,
May 30, 2012, 11:47:54 AM5/30/12
to testng...@googlegroups.com
Hi Krishnan,

The problem was data type mismatch, its sorted now. Thanks for pointing that out.

Cheers,
John
Reply all
Reply to author
Forward
0 new messages