Finally I found a set of tools which made my unit testing much more easier! In this article I am taking you through my unit testing strategies and how I managed to write unit test cases without writing a bunch of code.
The following are some of the technologies I use in my application:
1. EJB 3.0
2. Hibernate 3.0
3. JBoss 5.0.1
4. JMS (JBoss Messaging)
Let me explain in detail how OpenEJB + TestNG + jMockIt combination helped me in unit testing my code.
1. Defining a model class (UserInfo.java)
public class UserInfo implements java.io.Serializable { private String loginId; private String name; public String getLoginId() { return this.loginId; } public void setLoginId(String loginId) { this.loginId = loginId; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
2. Defining the Hibernate hbm file (UserInfo.hbm.xml)
<hibernate-mapping> <class name="com.sample.model.UserInfo" table="USER_INFO"> <id name="loginId" type="string"> <column name="LOGIN_ID" length="32" /> <generator class="assigned" /> </id> <property name="name" type="string"> <column name="NAME" length="50" /> </property> </class> </hibernate-mapping>
3. Data Access Object for UserInfo (UserInfoHibernateDAO.java)
public class UserInfoHibernateDAO { public UserInfo getUserInfo(String loginId){ Session session = HibernateUtil.getSessionFactory().openSession(); return (UserInfo)session.get(UserInfo.class, loginId); } }4. Creating a stateless EJB
@Stateless public class HelloBean implements Hello { public String getUserName(String loginId){ UserInfoHibernateDAO hibernateDAO = new UserInfoHibernateDAO(); UserInfo userInfo = hibernateDAO.getUserInfo(loginId); return userInfo.getName(); } }Unit Testing EJB3 + Hibernate Using OpenEJB + TestNG + jMockIt - Part II
Part II