3
public class DemoTest {

private static final ThreadLocal<WebDriver> webDriverThreadLocal= new InheritableThreadLocal<>();
private String baseUrl;
 Random random = new Random();
 int no;
 private final Map<Integer, String> map =new LinkedHashMap<Integer, String>();

@Test(dataProvider = "dp1")
public void f(Integer n, String s) {
 WebDriver driver = webDriverThreadLocal.get();
  no= random.nextInt(4);
 map.put(no, "1234567");
}

@Test(dependsOnMethods = {"f"}, dataProvider = "dp2")

  public void f1(Integer uname, String password){

  System.out.println("uname:"+uname+" password:"+password);
  System.out.println("In f1 id:"+Thread.currentThread().getId());
}

@BeforeMethod
 public void beforeMethod()  {

  WebDriver driver= new FirefoxDriver();
//  driver = ThreadGuard.protect(new FirefoxDriver());    
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  webDriverThreadLocal.set(driver);
  System.out.println("Before method id:"+Thread.currentThread().getId());

}

 @AfterMethod
  public void afterMethod() {
  WebDriver driver = webDriverThreadLocal.get();
      System.out.println("After method id:"+Thread.currentThread().getId());
      System.out.println("");
  if(driver != null ) {
  driver.quit();
 // driver.close();
  }

 }

 @DataProvider(name ="dp1" ,parallel=true)
 public Object[][] dp() {
 return new Object[][] {
  new Object[] { 1, "a" },
  new Object[] { 2, "b" },
  new Object[] { 3, "c" },
  new Object[] { 4, "d" },

  };
 }

 @DataProvider(name="dp2" , parallel=true)
 public Iterator dp2() {
   return this.map.entrySet().iterator();

 }
  • Test f1 is dependent on test f.
  • f1 generates random number & a string which is input to test f1.
  • I want to pass map Iterator from test f to test f1.

This is what I have tried so far.

I get the following error:

java.lang.ClassCastException: java.util.LinkedHashMap$Entry cannot be cast to [Ljava.lang.Object;

How to resolve this ?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Grishma Oswal
  • 303
  • 1
  • 7
  • 22
  • Where do you get the error exactly ? Which line ? Isn't because you're using a 2D array of Object in dp() ? – Asoub Dec 16 '15 at 14:45

1 Answers1

1

A TestNG data provider must return either Object[][] or Iterator<Object[]> but dp2() is returning neither.

this.map.entrySet().iterator() returns an Iterator<Map.Entry<Integer, String>>.

The error you are getting, java.lang.ClassCastException: java.util.LinkedHashMap$Entry cannot be cast to [Ljava.lang.Object;, is from TestNG trying to cast the first element returned by the iterator from dp2() to an Object[] but a Map.Entry<K, V> cannot be cast to an Object[].

i.e. You cannot use an iterator over the entry-set of a map as a TestNG data provider. You can, however, transform your data. e.g.:

@DataProvider(name = "dp2", parallel = true)
public Iterator<Object[]> dp2() {
    return FluentIterable.from(this.map.entrySet())
            .transform(new Function<Map.Entry<Integer, String>, Object[]>() {
                @Override
                public Object[] apply(Map.Entry<Integer, String> input) {
                    return new Object[]{input.getKey(), input.getValue()};
                }
            })
            .iterator();

}
mfulton26
  • 29,956
  • 6
  • 64
  • 88
  • This worked, but test f1 executes only twice instead of 4 – Grishma Oswal Dec 17 '15 at 08:34
  • Well you are using a map which means if you generate the same random number then you are overwriting the existing entry in the map and not adding a new one. Instead of a map you could use a list of Map.Entry, a list of Object[], or a guava multimap. Why are you creating a random number? If your trying to rearrange the order of the elements from dp1 then you could just build a list and then use Collections.shuffle. – mfulton26 Dec 17 '15 at 11:24
  • I am not creating random no now, so it works fine. But could you suggest an easy way of passing map to Test method – Grishma Oswal Dec 17 '15 at 13:25