News and Articles on Technology, Finance and Marketing

Unit Testing EJB3 + Hibernate Using OpenEJB + TestNG + jMockIt - Part II

In my previous blog I explained how to create a stateless bean, how to configure Hibernate model class, Hibernate hbm file and a DAO class for accessing data.

In this section, I will explain how to write test class for our stateless bean UserInfoBean.java.

 

1. Mock HibernateUtil.java
You can use existing HibernateUtil class or mock the class to include custom properties which is needed only for unit testing. A sample given below:
public class MockedHibernateUtil {
   private static SessionFactory sessionFactory;
   public static SessionFactory getSessionFactory() {
      if (null == sessionFactory) {
         sessionFactory = new Configuration().configure(
     new File("test-hibernate.cfg.xml")).buildSessionFactory();
      } 
      return sessionFactory;
   }
}
2. Mocked Service Locator
public class MockedServiceLocator {
   public static Object getEjb(String jndiName) throws Exception{
 InitialContext initialContext = null;
 Properties properties = new Properties();
 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  "org.apache.openejb.client.LocalInitialContextFactory");
 initialContext = new InitialContext(properties);
 return initialContext.lookup(jndiName);
   }
}
3. Mocker class
public class Mocker {
   public static void mockHibernateUtil() {
      Mockit.redefineMethods(HibernateUtil.class, MockedHibernateUtil.class);
   }
   public static void mockServiceLocator() {
      Mockit.redefineMethods(ServiceLocator.class, MockedServiceLocator.class);
   }
}
4. Test data provider class
public class UserInfoBeanTestDataProvider {
   @DataProvider(name = "testUserInfoDataProvider")
   public static Object[][] testUserInfoDataProvider() {
      return new Object[][] { { "john" }, { "tracy" }, { "leo" } };
   }
}
5. Test class
public class HelloBeanTest {
   private static InitialContext initialContext;
   
   @BeforeClass
   public void setUp() {
      Mocker.mockHibernateUtil(); 
      Mocker.mockServiceLocator(); 
   }

   @Test(groups = { "usergroup" }, dataProviderClass = 
   UserInfoBeanTestDataProvider.class, dataProvider = "testUserInfoDataProvider")
   public void testGetUserName(String arg) throws NamingException {
      Object obj = ServiceLocator.getEjb("jndi.UserInfo");
      assert (null != obj) : "obj is null";
      UserInfo userInfo = (UserInfo) obj;
      Assert.assertNotNull(userInfo.getUserName(arg));
   }

   @AfterClass
   protected void tearDown() {
      // clean up
   }
}

Go back to Part I

Share:

1 comment:

  1. Hi!

    I'm trying to do the same, but using jUnit 4 instead of TestNG.

    Why didn't you use the jMockit Hibernate3Emul to avoid mocking DAOs and the MOR engine? Any special issue?

    Regards!

    ReplyDelete

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to our feed and get articles like this delivered automatically to your feed reader? Like our Facebook Page.

About Us

About Us
We provide you the latest news on technology, finance and entertainment across the globe. Fell free to contact us for any questions or concerns.

Popular Posts

Blog Archive

Recent Posts

Definition List

Definition list
Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Lorem ipsum dolor sit amet
Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Unordered List

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  • Vestibulum auctor dapibus neque.

Pages

Theme Support

Need our help to upload or customize this blogger template? Contact me with details about the theme customization you need.