-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: strategy of unit tests using Hibernate
PostPosted: Fri Aug 31, 2007 3:28 pm 
Newbie

Joined: Fri Aug 31, 2007 3:08 pm
Posts: 1
I am starting with unit tests using Hibernate and I´ve some question about the right strategy to proceed this tests. At first I researched mock objects and stubs, then I realized that I could instantiate a Hibernate session and access my database, like the code below:

The Test Class
Code:
public class UserDAOHibernateTest extends TestCase{
   UserDAO uDAO;
   User u;
   Session session;
   
   @Before
   public void setUp() throws Exception {
      session = HibernateUtil.getSessionFactory().getCurrentSession();
      uDAO = DAOFactory.instance(DAOFactory.HIBERNATE).getUserDAO();
      session.beginTransaction();
      u = new User();
      u.setId("22222222222");
      u.setSenha("7560181bc399878837802c283a2d1dbd");
      u.setEmail("username@gmail.com");
      session.saveOrUpdate(u);
   }

   @After
   public void tearDown() throws Exception {
      session.delete(u);
      session.getTransaction().commit();      
   }

   @Test
   public void testAuthenticateUser() {
      User test1 = uDAO.authenticateUser(u.getId(),u.getPassword());
      assertNotNull("User not found in DB",test1);
      //assertTrue(test1.equals(u));
      assertSame(test1, u);      
   }


The Class to Be Tested
Code:
public class UserDAOHibernate
      extends GenericHibernateDAO<User, Integer>
      implements UserDAO {
   
   public User autenticaUsuario(String id, String password) {
                  
      Criteria crit = getSession().createCriteria(getPersistentClass());
      crit.add(eq("id",id));
      crit.add(eq("password",password));
      
      return (User) crit.uniqueResult();
   }


I dont know if this kind of test code is apropriated. How is the best way to do it?

thanks[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 01, 2007 6:03 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
This link might be interesting for you:
http://www.theserverside.com/tt/articles/article.tss?l=UnitTesting


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.