[jboss-user] [JBoss Seam] - Re: Entity EJB3.0 Unit Testing - How to setup JNDI datasourc

sushmu do-not-reply at jboss.com
Thu Dec 27 02:16:06 EST 2007


Finally Integration testing of EJB works with Maven!

I have 3 modules as described in Maven guide.

1. EJB
2. WAR
3. EAR

Entity beans testing is done in EJB module as described in previous post.

Session beans testing is done in WAR module since it requires the servlet/facelet functionality. One additional step on top of the previous explanation was to copy 'components.xml' in 'target/test-classes/WEB-INF' folder.

The Action to test:

package com.ibt.intellidocs.dummy;
  | 
  | import static org.jboss.seam.ScopeType.EVENT;
  | import org.jboss.seam.log.Log;
  | 
  | import javax.persistence.EntityManager;
  | import java.io.Serializable;
  | import java.util.List;
  | 
  | @Stateful
  | @Scope(EVENT)
  | @Name("dummyUserAction")
  | public class DummyUserAction
  |         implements IDummyUserLocal, Serializable
  | {
  |     @Logger
  |     Log log;
  | 
  |     @In(required = true)
  |     private DummyUser dummyUser;
  | 
  |     @PersistenceContext
  |     private EntityManager entityManager;
  | 
  |     private String verify;
  |     private boolean registered;
  | 
  |     public DummyUser findDummyUser(long id)
  |     {
  |         return entityManager.find(DummyUser.class, id);
  |     }
  | 
  |     public long register()
  |     {
  |         if (dummyUser.getPassword().equals(verify))
  |         {
  |             List existing = entityManager.createQuery
  |                     ("select u.username from DummyUser u where u.username=#{dummyUser.username}")
  |                     .getResultList();
  |             if (existing.size() == 0)
  |             {
  |                 entityManager.persist(dummyUser);
  |                 log.info("Username #{dummyUser.username} already exists");
  |                 registered = true;
  |             }
  |             else
  |             {
  |                 log.info("Username #{dummyUser.username} already exists");
  |             }
  |         }
  |         else
  |         {
  |             log.info("Re-enter your password");
  |             verify = null;
  |         }
  |         return dummyUser.getId();
  |     }
  | 
  |     public String getVerify()
  |     {
  |         return verify;
  |     }
  | 
  |     public void setVerify(String verify)
  |     {
  |         this.verify = verify;
  |     }
  | 
  |     @Destroy
  |     @Remove
  |     public void destroy()
  |     {
  |         log.info("dummySeamBean#destroy.. Done!");
  |     }
  | 
  |     public DummyUser getDummyUser()
  |     {
  |         return dummyUser;
  |     }
  | 
  |     public void setDummyUser(DummyUser dummyUser)
  |     {
  |         this.dummyUser = dummyUser;
  |     }
  | }
  | 

The Test:

package com.ibt.intellidocs.dummy;
  | 
  | import org.jboss.seam.mock.BaseSeamTest;
  | import org.jboss.seam.mock.SeamTest;
  | import org.testng.annotations.Test;
  | 
  | import java.util.logging.Logger;
  | 
  | public class DummyUserActionTest
  |         extends SeamTest
  | {
  | 
  |     Logger log = Logger.getLogger(DummyUserActionTest.class.getName());
  | 
  |     @Test
  |     public void testSeamComponents()
  |             throws Exception
  |     {
  |         new BaseSeamTest.FacesRequest("/dummy/registerDummyUser.xhtml")
  |         {
  |             @Override
  |             protected void invokeApplication()
  |                     throws Exception
  |             {
  |                 setValue("#{dummyUser.name}", "test1");
  |                 setValue("#{dummyUser.username}", "test1");
  |                 setValue("#{dummyUser.password}", "test1");
  |                 setValue("#{dummyUserAction.verify}", "test1");
  |                 Object id = invokeAction("#{dummyUserAction.register}");
  |                 log.info("TEST: Registred User with id: " + id);
  |             }
  |         }.run();
  | 
  |     }
  | 
  | }

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4115639#4115639

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4115639



More information about the jboss-user mailing list