method in handleUsersController
Code:
public boolean saveEditedUser( Users user ){
try {
// Get session from session factory
Session session = HibernateSessionFactory.currentSession();
// Starting a new transaction before interacting with the database
Transaction transaction = session.beginTransaction();
// Tell hibernate to save to database, gets the object ID in return
session.saveOrUpdate( user );
// Commit to save changes
transaction.commit();
// Closing session
HibernateSessionFactory.closeSession();
return true;
method in JUnit testcaseCode:
public void testSaveEditedUser(){
Users user = new Users();
user.setUserID( new Integer( 483) );
user.setLastName( "saveOrUpdate" );
user.setFirstName( "test" );
user.setUserName( "test" );
user.setUserPass( "test" );
user.setGroupID( 1 );
// group id m? hentes inn her fra liste "gruppe"
user.setAddress( "test" );
user.setHomePhone( "test" );
user.setCellPhone( "test" );
user.setEmail( "test" );
user.setPosition( "test" );
user.setLanguage("no"); // m? hentes inn fra brukerpreferanse session.getParameter("language");
user.setPostNo( 55 );
user.setPostRegion( "test" );
user.setWorkPhone( "test" );
user.setIsActive( 1 );
user.setIsDeleted( 0 );
// Send the user to be created in database, returns PrimaryKey of user
boolean userSaved = handleUsersController.saveEditedUser( user );
// Checks if the method returned true i.e was succesfully executed.
assertEquals( true, userSaved );
}
}