Frage

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.

War es hilfreich?

Lösung

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top