views:

57

answers:

1

Hi guy,

I work on an application which use Seam and Hibernate (with Jboss 5.1 server).

I have 4 projects in my eclipse (Portail, Portail-ear, Portail-ejb and Portail-test). When I start the application, all work fine (I can go in localhost:8080).

But when I want to realise some tests in the test project, I have a problem. Here is the code for the user creation test:

public class UserTest extends SeamTest {  

 @PersistenceContext
 IdentityManager identityManager;

 @PersistenceContext
 private EntityManager entityManager;

 @Test  
 public void testUserCreate() throws Exception {  
         new ComponentTest() {  
             @Override  
             protected void testComponents() throws Exception {

  Log log = Logging.getLog(UserTest.class);
  Log testLog = Logging.getLog(Log.class);

                (UserAction)Component.getInstance("userAction", true);
  setField(ua, "entityManager", entityManager);
  setField(ua, "identityManager", identityManager);
  setField(ua, "log", testLog);
  ua.setCreation(true);
  ua.setConfirm("toto");
  Portailrole portailrole = null;
  try{
      log.info("/****** CHERCHE ROLE ******/");
      if (entityManager == null)
      log.info("entityManager is null");
      Query query = entityManager.createQuery("from Portailrole where name=:rolename").setParameter("rolename", "Admin");
                    portailrole = (Portailrole) query.getSingleResult();
      log.info("/****** FIN CHERCHE ROLE ******/");
  } 
  catch (Exception e) {
           log.info("Exception portailrole {0} {1}", e.getClass(), e.getMessage());
  }
  log.info("testUserCreate : {0}", "role trouvé?");
  Set<Portailrole> portailroles = new HashSet<Portailrole>();
  if (portailrole != null) { 
   log.info("testUserCreate : {0}", portailrole.getName());
   portailroles.add(portailrole);
  }

  Portailuser testUser = new Portailuser(new Long(99999999),"nom","prenom","0123455689","0123455689", "0123455689","[email protected]","login", "toto","FFEFAA", true, portailroles);
  log.info("testUserCreate : {0}", "user créé");
  ua.setUser(testUser);
  if ("failure".equals(ua.save())) {
            log.info("testUserCreate : {0}", "echec du save");
   assert false;
  }
  log.info("testUserCreate : {0}", "user sauvé");
         }.run();  
     }   

And my entityManager is null. I think the @PersistenceContext annotation doens't work in my case but I don't understand why.

My hibernate configuration is in the EJB project in META-INF/persistence.xml

Someone has an idea to help me ?

Thanks.

+1  A: 

Retrieve the EntityManager inside the ComponentTest.testComponents() method using Component.getInstance() instead of trying to inject it:

EntityManager em = (EntityManager) Component.getInstance("entityManager");
germanescobar