我在使用Seam和Hibernate(使用Jboss 5.1服务器)的应用程序上工作。
我的日食中有4个项目(Portail,Portail-ear,Portail-ejb和Portail-test)。 当我启动应用程序时,一切正常(我可以进入localhost:8080)。
但是当我想在测试项目中实现一些测试时,我遇到了问题。以下是用户创建测试的代码:
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();
}
我的entityManager为null。我认为@PersistenceContext注释在我的情况下不起作用,但我不明白为什么。
我的hibernate配置位于META-INF / persistence.xml
中的EJB项目中有人有想法帮助我吗?
感谢。
答案 0 :(得分:1)
使用Component.getInstance()检索ComponentTest.testComponents()方法中的EntityManager,而不是尝试注入它:
EntityManager em = (EntityManager) Component.getInstance("entityManager");