Venom February 2016

Convert Queue<Deque<String>> into Object[][] in Java

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: net.easysol.detectid.migrationtester.ArrayDeque cannot be cast to java.lang.String

Answers


Sasha Salauyou February 2016

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.

Post Status

Asked in February 2016
Viewed 2,051 times
Voted 14
Answered 1 times

Search




Leave an answer