Basic dataprovider question (Access individual object values)?

28 views
Skip to first unread message

magnatron

unread,
Sep 12, 2006, 4:39:06 PM9/12/06
to testng...@googlegroups.com
I have the following class:

@Test
public class IdmTestRootRealm extends IdmGetSSOToken
{
protected ResourceBundle rb = null;

@DataProvider(name = "am")
public Object[][] createData() {
rb = ResourceBundle.getBundle("am");
return new Object [][] {
{ "amadmin.user", rb.getString("amadmin.user") },
{ "amadmin.password", rb.getString("amadmin.password") },
{ "basedn", rb.getString("basedn") },
};
}

@Test(dataProvider="am",groups={"server"})
public void TestRootRealm(String s1, String s2)
{
System.out.println(s1 + " " + s2);
}
}

When I execute this, I get the following output:

[testng] amadmin.user amadmin
[testng] amadmin.password secret12
[testng] basedn dc=red,dc=iplanet,dc=com

This is all correct. But how can I access the dataprovider values individually. What I want to do is something like this:

@Test(dataProvider="am",groups={"server"})
public void TestRootRealm(String s1, String s2)
{
String a1 = 1st value in s2
String a2 = 2st value in s2
String a3 = 3st value in s2
}

Basically how do I access the individual elements of the array object. Please let me know if I am not clear in my question. Thanks
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=43000&messageID=86263#86263

Cédric Beust ♔

unread,
Sep 12, 2006, 4:47:43 PM9/12/06
to testng...@googlegroups.com
I would suggest changing your test method so it accepts strings of these arrays instead of individual values:


   @Test(dataProvider="am",groups={"server"})
   public void TestRootRealm(String[] s1, String[] s2)
   {
       String a1 = s2[0];
       String a2 = s2[1];
       String a3 = s2[2];
   }

Then you can invoke another method that only accepts individual values.  Of course, you also need to modify your dataprovider to match the new signature.

--
Cedric




On 9/12/06, magnatron <testng...@opensymphony.com> wrote:

I have the following class:

@Test
public class IdmTestRootRealm extends IdmGetSSOToken
{
    protected ResourceBundle rb = null;

    @DataProvider(name = "am")
    public Object[][] createData() {
        rb = ResourceBundle.getBundle("am");
        return new Object [][] {
            { "amadmin.user", rb.getString("amadmin.user") },
            { "amadmin.password", rb.getString("amadmin.password") },
            { "basedn", rb.getString ("basedn") },

magnatron

unread,
Sep 12, 2006, 6:42:30 PM9/12/06
to testng...@googlegroups.com
I am not clear. I did the following and am getting an error:

@Test
public class IdmTestRootRealm extends IdmGetSSOToken
{
protected ResourceBundle rb = null;

@DataProvider(name = "am")
public Object[][] createData() {
rb = ResourceBundle.getBundle("am");
return new Object [][] {
{ "amadmin.user", rb.getString("amadmin.user") },
{ "amadmin.password", rb.getString("amadmin.password") },

{ "basedn", rb.getString("basedn") },
};
}

@Test(dataProvider="am",groups={"server"})
public void TestRootRealm(String[] s1, String[] s2)
{

System.out.println("BEFORE PRINT");
System.out.println(s2[0]);
System.out.println(s2[1]);
System.out.println(s2[2]);
System.out.println("AFTRE PRINT");


}
}
---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=43000&messageID=86278#86278

Cédric Beust ♔

unread,
Sep 12, 2006, 7:26:27 PM9/12/06
to testng...@googlegroups.com
Yes, it gets a bit more tricky because now, each line must return an array of strings.  Something like this:

        return new Object [][] {
            {
              new String[] { "amadmin.user", " amadmin.password", "basedn" },
              new String [] {rb.getString("amadmin.user"), rb.getString("amadmin.password") }, rb.getString("basedn") }
            }
        };

Note that the test methods that receives the arrays will only be called *once*, which kind of defeats the purpose of data providers, but I suppose you have a good reason to want to know what all these values are ahead of time...

--
Cedric


On 9/12/06, magnatron <testng...@opensymphony.com> wrote:

I am not clear. I did the following and am getting an error:

@Test
public class IdmTestRootRealm extends IdmGetSSOToken
{
    protected ResourceBundle rb = null;

    @DataProvider(name = "am")
    public Object[][] createData() {
        rb = ResourceBundle.getBundle("am");
        return new Object [][] {
            { "amadmin.user", rb.getString("amadmin.user") },
            { "amadmin.password", rb.getString("amadmin.password") },
            { "basedn", rb.getString("basedn") },
        };
    }

    @Test(dataProvider="am",groups={"server"})
    public void TestRootRealm(String[] s1, String[] s2)
     {
        System.out.println("BEFORE PRINT");
        System.out.println(s2[0]);
        System.out.println(s2[1]);
        System.out.println (s2[2]);

        System.out.println("AFTRE PRINT");
    }
}
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=43000&messageID=86278#86278







--
Cédric

magnatron

unread,
Sep 12, 2006, 8:53:54 PM9/12/06
to testng...@googlegroups.com
Well so far no success. I tried the following:

@Test
public class IdmTestRootRealm extends IdmGetSSOToken
{
protected ResourceBundle rb = null;

@DataProvider(name = "am")
public Object[][] createData() {
rb = ResourceBundle.getBundle("am");
return new Object [][] {

new String[] { "amadmin.user", "amadmin.password", "basedn" },
new String [] {rb.getString("amadmin.user"), rb.getString("amadmin.password") , rb.getString("basedn") }
};
}

@Test(dataProvider="am",groups={"server"})


public void TestRootRealm(String[] s1, String[] s2)

throws IdRepoException, SSOException


{
System.out.println("BEFORE PRINT");
System.out.println(s2[0]);
System.out.println(s2[1]);

System.out.println(s2[2]);
System.out.println("AFTRE PRINT");
}
}

I get the following exception in the report:

java.lang.IllegalArgumentException: wrong number of arguments
... Removed 17 stack frames

Click to show all stack frames

java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:552)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:411)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:785)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:114)
at org.testng.TestRunner.privateRun(TestRunner.java:695)
at org.testng.TestRunner.run(TestRunner.java:574)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:241)
at org.testng.SuiteRunner.run(SuiteRunner.java:145)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:901)
at org.testng.TestNG.runSuitesLocally(TestNG.java:863)
at org.testng.TestNG.run(TestNG.java:613)
at org.testng.TestNG.privateMain(TestNG.java:1001)
at org.testng.TestNG.main(TestNG.java:938)

Appreciate the help. Thanks


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=43000&messageID=86289#86289

Mark Derricutt

unread,
Sep 12, 2006, 9:08:39 PM9/12/06
to testng...@googlegroups.com
Um, I see the data provider has an array of THREE elements (user, password, basedn) but the TestRootRealm() method only takes TWO parameters...

Cédric Beust ♔

unread,
Sep 12, 2006, 10:07:07 PM9/12/06
to testng...@googlegroups.com
Change your method to receive only one array of strings, get the dataprovider right, then add the second parameter.

--
Cedric


On 9/12/06, magnatron <testng...@opensymphony.com> wrote:
        at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.testng.internal.MethodHelper.invokeMethod (MethodHelper.java:552)

        at org.testng.internal.Invoker.invokeMethod(Invoker.java:411)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:785)
        at org.testng.internal.TestMethodWorker.run (TestMethodWorker.java:114)

flipper

unread,
Sep 12, 2006, 11:39:28 PM9/12/06
to testng-users
After looking at the source of testNG, dataprovider implmentation
basically feed the test method with a array of dataset ONE AT A TIME,
i.e. you cannot have access to all the data like what you have done in
your code unless we do it in this manner

import java.util.*;
import org.testng.annotations.*;

public class IdmTestRootRealm {

@DataProvider(name = "am")
public Object[][] createData() {

Map<String, String> map = new HashMap<String, String>();
map.put("amadmin.user", "amadmin");
map.put("amadmin.password", "1111111");
map.put("basedn", "dc=opensso,dc=com");
return new Object [][] {
{ "dataset1", map }
};
}

@Test(dataProvider="am", groups={"server"})
public void TestRootRealm(String key, Map<String, String> data) {
for (String k : data.keySet()) {
System.out.println(k + "=" + data.get(k));
}
}
}

Then you can have different set of amadmin, password, and basedn
accordingly.

magnatron

unread,
Sep 13, 2006, 3:34:49 PM9/13/06
to testng...@googlegroups.com
That did the trick. Thanks all for all the help. I appreciate it very much. Excellent response.

---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=43000&messageID=86522#86522

Reply all
Reply to author
Forward
0 new messages