简体   繁体   English

接缝和休眠(JPA)问题测试

[英]Problem test with seam and hibernate (JPA)

I work on an application which use Seam and Hibernate (with Jboss 5.1 server). 我在使用Seam和Hibernate(带有Jboss 5.1服务器)的应用程序上工作。

I have 4 projects in my eclipse (Portail, Portail-ear, Portail-ejb and Portail-test). 我的月食中有4个项目(Portail,Portail-ear,Portail-ejb和Portail-test)。 When I start the application, all work fine (I can go in localhost:8080). 当我启动应用程序时,一切正常(我可以进入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. 而我的entityManager为null。 I think the @PersistenceContext annotation doens't work in my case but I don't understand why. 我认为@PersistenceContext注释在我的情况下不起作用,但我不明白为什么。

My hibernate configuration is in the EJB project in META-INF/persistence.xml 我的休眠配置位于META-INF / persistence.xml中的EJB项目中

Someone has an idea to help me ? 有人有办法帮助我吗?

Thanks. 谢谢。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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