My method is having following line -
ObjectMapper objectMapper = new ObjectMapper(); //1
JsonNode root = objectMapper.readTree(details); //2
((ObjectNode) root).put("userId", userId); //3
ObjectMapper mockMapper = PowerMockito.mock(ObjectMapper.class);
java.lang.ClassCastException: com.fasterxml.jackson.databind.JsonNode$$EnhancerByMockitoWithCGLIB$$26691c0b cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
So your mocking framework will make sure that mockJsonNode is "exactly" of that type JsonNode.
Keep in mind: a cast is nothing else but telling the compiler "he, you compiler, that object X that you think has type Y, in reality it has type Z". In order to make that work, X actually must be a Z at runtime.
So, the simple solution could be to change that declaration to:
In other words: your production code assumess that the result returned by
readTree() is actually an instance of ObjectNode. Then, of course you have to make sure that your mocking framework actually returns something that is an ObjectNode.