Hi everyone!
I'm very new to using mockito, and I'm having a hard time figuring out
how to mock hibernate classes so I can test GWT services. This is the
method I want to test:
public ExpertiseArea saveExpertiseArea(ExpertiseArea ea) {
try{
expertiseAreaValidator.assertValid(ea);
}catch(InvalidStateException ise){
invalidValues = expertiseAreaValidator.getInvalidValues(ea);
}
try{
dao.setSession(getSessionFactory().getCurrentSession());
dao.getSession().beginTransaction();
ExpertiseArea result = dao.makePersistent(ea);
return result;
}catch(HibernateException he){
Log.debug(he.getMessage());
}finally{
dao.getSession().getTransaction().commit();
}
return null;
}
I don't want the test to depend on the dao, so I'm trying to mock out
the dependencies:
public void setUp(){
service = (AOEServiceImpl)appContext.getBean("AOEServiceImpl");
serviceSpy = spy(service);
ExpertiseAreaDAO dao = service.getDao();
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
when(sessionFactory.getCurrentSession()).thenReturn(session);
stub(serviceSpy.getSessionFactory()).toReturn(sessionFactory);
}
I'm using a spy here to return a mocked SessionFactory. Then the
actual test:
public void testEntityIsValidatedCorrectly(){
ExpertiseArea ea = new ExpertiseArea("TestArea");
assertNotNull("Entity should not be null", ea);
ea = serviceSpy.saveExpertiseArea(ea);
assertNotNull("Entity should not be null", ea);
ea = new ExpertiseArea();
ea.setName(null);
assertNull("Entity should not validate",
service.saveExpertiseArea(ea));
}
The problem is that, when I call serviceSpy.saveExpertiseArea() and
that in turn calls it's getSessionFactory(), the real
getSessionFactory() is called, throwing a NullPointerException instead
of returning the mocked sessionFactory.
The getSessionFactory() method is inherited from a base class, called
BaseServiceImpl, maybe that's important.
Any suggestion is greatly appreciated. Thanks in advance!
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to
moc...@googlegroups.com.
To unsubscribe from this group, send email to
mockito+u...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mockito?hl=en.