I am testing a service which returns a List of Object[]
List<Object[]> list = someService.someMethod()
list.each { Object[] row ->
assertEquals(A_SIZE,row.length)
}
I double checked through the debugger, list's element are Object[8], but row is initialized as an Object[1], which contains an Object[8], so the test fails unless A_SIZE is 1 (which is meaningless in my scenario). If I change the assert to:
assertEquals(A_SIZE, row[0].length)
the test goes fine, but I don't understand why I need the "[0]", since "row" is supposed to be the Object[8], as far as I understand from Groovy specifications.
I am using groovy 2.1 and grails 2.3M2; the list comes from a MyDomainClass.createCriteria().
I think Groovy has troubles working out what to do to map the Object[]
to the Closure parameter. A workaround is to not declare the type of the Closure parameter like so:
list.each { row ->
assertEquals(A_SIZE,row.length)
}
I've asked around, and I think this is because Groovy currently tries to 'fit' the object to the Closure parameters (in the way that this works)
// Returns [ 3, 7 ]
[ [ 1, 2 ], [ 3, 4 ] ].collect { a, b ->
a + b
}
There have been debates as to whether Groovy should do this sort of destructuring or not on the mailing list, but for now, it does :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.