Gobliins
Gobliins

Reputation: 4026

PowerMock / Mockito casting mocked objects not working

I initialize in my test:

this.sessionMock = mock(Session.class);

And in the method under test the line:

((MySession)session).setRecordLimits(recordLimits)

gives me:

 java.lang.ClassCastException:
 org.hibernate.Session$$EnhancerByMockitoWithCGLIB$$8561a329 
 cannot be cast to myApp.MySession

The class:

public class MySession extends AbstractSessionImpl implements EventSource

Is it a problem of Mockito / Powermock or is it a Problem with Hibernate? And is there any fix for this?

Upvotes: 0

Views: 3471

Answers (1)

Adrian Shum
Adrian Shum

Reputation: 40036

By telling Mockito/Powermock that you want to mock Session, it has no way to know the class you want to mock is actually MySession.

Due to your code actually depends on a MySession, you should do

this.sessionMock = mock(MySession.class);

instead

Upvotes: 3

Related Questions