简体   繁体   中英

Problem test with seam and hibernate (JPA)

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","toto@toto.fr","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.

使用Component.getInstance()检索ComponentTest.testComponents()方法内的EntityManager,而不是尝试注入它:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM