public String createUser() {
Session session = HibernateUtility.getSession();
Transaction tx = session.beginTransaction();
session.save(this);
tx.commit();
session.close();
return "accountCreated";
}
How to write Unit test case for this method?
It is difficult to unit test code which contains calls to static methods, like
HibernateUtility.getSession().You might consider injecting the Session instance (perhaps by adding a parameter to the
createUser()method, or by giving the object that contains this method aSession Factoryobject which it can use to obtainSessioninstances). You could then use Mockito or a similar framework to provide a mockSessionobject to the method, and verify that the expected calls where being made to it.Using Mockito, a unit test might look like this: