Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account

I'm getting below error:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account 

with below code

final int expectedId = 1;  Test newTest = create();  int expectedResponseCode = Response.SC_OK;  ArrayList<Account> account = given().when().expect().statusCode(expectedResponseCode)     .get("accounts/" + newTest.id() + "/users")     .as(ArrayList.class); assertThat(account.get(0).getId()).isEqualTo(expectedId); 

Is there a reason why I cannot do get(0)?

like image 555
Passionate Engineer Avatar asked Mar 03 '15 00:03

Passionate Engineer


People also ask

How do I resolve Java Lang ClassCastException?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

How do you avoid ClassCastException in your code?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What causes ClassCastException?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

How do I convert a map to LinkedHashMap?

To convert all the values of a LinkedHashMap to a List in Java, we can use the values() method. The values() is a method of the LinkedHashMap that returns a Collection of all the values in the map object. We can then convert this collection to a List object.


1 Answers

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 Accounts. 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); 
like image 66
Mark Peters Avatar answered Sep 23 '22 07:09

Mark Peters