java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account
Solution 1:
The issue's coming from Jackson. When it doesn't have enough information on what class to deserialize to, it uses LinkedHashMap
.
Since you're not informing Jackson of the element type of your ArrayList
, it doesn't know that you want to deserialize into an ArrayList
of Account
s. So it falls back to the default.
Instead, you could probably use as(JsonNode.class)
, and then deal with the ObjectMapper
in a richer manner than rest-assured allows. Something like this:
ObjectMapper mapper = new ObjectMapper();
JsonNode accounts = given().when().expect().statusCode(expectedResponseCode)
.get("accounts/" + newClub.getOwner().getCustId() + "/clubs")
.as(JsonNode.class);
//Jackson's use of generics here are completely unsafe, but that's another issue
List<Account> accountList = mapper.convertValue(
accounts,
new TypeReference<List<Account>>(){}
);
assertThat(accountList.get(0).getId()).isEqualTo(expectedId);
Solution 2:
Try the following:
POJO pojo = mapper.convertValue(singleObject, POJO.class);
or:
List<POJO> pojos = mapper.convertValue(
listOfObjects,
new TypeReference<List<POJO>>() { });
See conversion of LinkedHashMap for more information.
Solution 3:
The way I could mitigate the JSON Array to collection of LinkedHashMap objects problem was by using CollectionType
rather than a TypeReference
.
This is what I did and worked:
public <T> List<T> jsonArrayToObjectList(String json, Class<T> tClass) throws IOException {
ObjectMapper mapper = new ObjectMapper();
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, tClass);
List<T> ts = mapper.readValue(json, listType);
LOGGER.debug("class name: {}", ts.get(0).getClass().getName());
return ts;
}
Using the TypeReference
, I was still getting an ArrayList of LinkedHashMaps, i.e. does not work:
public <T> List<T> jsonArrayToObjectList(String json, Class<T> tClass) throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<T> ts = mapper.readValue(json, new TypeReference<List<T>>(){});
LOGGER.debug("class name: {}", ts.get(0).getClass().getName());
return ts;
}
Solution 4:
I had a similar exception (but different problem) - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.bson.Document
, and fortunately it's solved easier:
Instead of
List<Document> docs = obj.get("documents");
Document doc = docs.get(0)
which gives error on second line, One can use
List<Document> docs = obj.get("documents");
Document doc = new Document(docs.get(0));