2

I'm building a dynamic test with @DataProvider annotation for TestNG. In Java, how to convert the data structure defined as Queue<Deque<String>> queueOfDeques = ArrayDeque<Deque<String>>(); to an Object[][]? Based on this explanation, I tried this:

@DataProvider( name = "providedQueue" )
public static Object[][] dataForTest(){
        return new Object[][]{{SomeClass.GetQueueOfDeques}};
    }

Where the method GetQueueOfDeques returns the queueOfDeques data structure defined above. I don't know what I'm doing wrong, but it is not converting the variable as it should be, deriving that TestNG ignores the parameterized test.

java.lang.ClassCastException: com.company.product.migrationtester.ArrayDeque cannot be cast to java.lang.String

Nana89
  • 432
  • 4
  • 21
  • 1
    I know it's not my business... but why should you want to downgrade to an Object[][] collection? You will still need to re-cast its content afterwards to be able to access the elements methods :/ – Diego Martinoia Feb 08 '16 at 15:31
  • 1
    @DiegoMartinoia this is returning type which `@DataProvider` is applicable for. – Alex Salauyou Feb 08 '16 at 15:46

1 Answers1

2

Simply do it by iteration:

static Object[][] convert(Collection<? extends Collection<?>> cc) {
    Object[][] res = new Object[cc.size()][]; 
    int i = 0;
    for (Collection<?> c : cc)
        res[i++] = c.toArray();
    return res;
}

Such you get general method for converting any collection of collections (they may be Queue, Deque, List and many others) of any type (as <?> refers) to Object[][] array.

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • I changed the `res[i++] = c.toArray();` to `res[i++] = Object[ ] {c}` and worked perfectly. ty so much @sasha-salauyou – Nana89 Feb 08 '16 at 16:47