문제

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.

도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top