Question

I have a object which I mock using powermock while unit testing.

ClassA mockedClassA = PowerMock.createMock(ClassA.class);

Later in mycode I'm using that object with a cast. Life follows.

String hello = ((ClassB)classA).someMethod();

Junit test throws following exception at thie place.

java.lang.ClassCastException: $Proxy15 incompatible with ClassB

It looks like casting the mocked object is not possible. So how can I go through this?

I tried following thigs.

Easymock.expect((ClassB)mockedClassA.somemethod())...;

doesn't work.

Was it helpful?

Solution

Your code expects classA to be a ClassB object, but it is not (unless ClassA extends ClassB, but then you wouldn't need the cast). Have you tried

ClassA mockedClassA = PowerMock.createMock(ClassB.class)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top