I come from Java background and am new to Groovy-Spock testing. I need to convert a list in Json to a List. I cannot share the exact code (proprietary), but here is in essence what is going on.
emp_test_data.json
[{
"empID":"1234",
"location":"HQ"
},
{
"empID":"5678",
"location":"Warehouse"
}]
Groovy code
List<Employee> employeeList = slurper.parse(new File("src/test/resources/data/emp_test_data.json"))
println ("After slurped") //prints
println(pickupVOList.size() //prints 2
println(pickupVOList.get(0)) //prints [empID:5678, location:HQ]
empUtil.processEmployees(employeeList)
EmpUtil.java
public void processEmployees(List<Employee> employeeList) {
for (Employee employee: employeeList) { //THIS FAILS!
//Do some processing
}
return;
}
The error I get is:- java.lang.ClassCastException: groovy.json.internal.LazyMap cannot be cast to com.my.domain.Employee
I've looked around quite a bit. In Groovy, there must be the right way of converting json to List of exact objects, not a list of LazyMap, but I cannot seem to find it.
Any ideas?