PowerMock and Hibernate

371 views
Skip to first unread message

Kartik Kumar

unread,
Apr 23, 2010, 3:20:01 AM4/23/10
to PowerMock
Hi,

I am trying to mock Hibernate' SessionFactory with PowerMock. In the
code that I am working on, the SessionFactory is built in the Dao
object constructor. I am doing something lke this :

PowerMock.suppress(PowerMock.constructor(Dao.class));
Dao dao = new MyDao();
SessionFactory sessionFactory =
PowerMock.createMock(SessionFactory.class);
Whitebox.setInternalState(dao, "sessionFactory", sessionFactory);

to stub out the code

public class MyDao extends Dao {}
public class Dao {
public Dao() {
this.sessionFactory = new
Configuration().configure().buildSessionFactory();
}

private SessionFactory sessionFactory;
}

Is this the right approach to stub this method? If it is, then I am
having problems with PowerMock 1.3.7 which uses javaassist v3.10 and
Hibernate 3.5 which uses javaassist v309. I am a mismatched class
version exception when I try to suppress the constructor. I am looking
at an earlier PowerMock release but if there is any way I can resolve
this issue without using an earlier version, please let me know.

Kartik

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

Johan Haleby

unread,
Apr 23, 2010, 12:03:39 PM4/23/10
to powe...@googlegroups.com
Hi,

Hmm.. It's hard to say what's causing it. What are you preparing for test? You should use javassist 3.10 with PowerMock (we'll try upgrading to 3.12 soon). Another possibility would be to just do Whitebox.newInstance(MyDao.class) and then set the session factory mock as you're doing right now, i.e:

Dao dao = Whitebox.newInstance(MyDao.class);
SessionFactory sessionFactory = PowerMock.createMock(SessionFactory.class);
Whitebox.setInternalState(dao, "sessionFactory", sessionFactory);

/Johan

Kartik Kumar

unread,
Apr 23, 2010, 12:45:00 PM4/23/10
to powe...@googlegroups.com
I am preparing MyDao.class for test.

Kartik Kumar

unread,
Apr 23, 2010, 12:48:18 PM4/23/10
to powe...@googlegroups.com
Exact error message

java.lang.RuntimeException: Can't invoke method public void com.jbe.powermock.MyDaoTest.testGetById_powermock(com.jbe.powermock.ActionItemTracking) throws java.lang.Exception on this instance of class com.jbe.powermock.MyDaoTest_$$_javassist_0 due to class mismatch
    at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:629)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:546)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:700)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1002)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:137)

Johan Haleby

unread,
Apr 23, 2010, 1:43:14 PM4/23/10
to powe...@googlegroups.com
Have you tried Whitebox.newInstance(..)?

/Johan

Kartik Kumar

unread,
Apr 23, 2010, 2:53:00 PM4/23/10
to powe...@googlegroups.com
Hi Johan,

It worked.

Thanks,

Kartik
Reply all
Reply to author
Forward
0 new messages