我使用JPA为我的DAO类编写测试,使用Hibernate作为JPA提供程序,以及Spring 3.2.我无法正确注入实体管理器,尝试访问它时会出现NullPointerException.我的GenericDAO实现如下所示:
@Repository
public class GenericDAOImpl implements GenericDAO {
@PersistenceContext(unitName="unitname")
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
// NullPointerException when calling this,entityManager is null
public Query createNamedQuery(String name) {
return entityManager.createNamedQuery(name);
}
// many other methods....
}
测试类看起来像这样:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/com/main/resources/root-context.xml","/com/main/resources/servlet-context.xml"})
public class TestModel {
@Before
public void setUp() throws Exception{
...
}
@Test
public void test(){
...
}
}
我的root-context.xml如下:
MysqL.jdbc.Driver" />
factorybean">
sql" value="true" />
MysqL5Dialect" />
我已经尝试了几种方法但没有成功,甚至按照其他SO问题的建议添加了PersistenceAnnotationBeanPostProcessor.所有其他的东西似乎工作正常:Hibernate创建数据库表,加载上下文等.我做错了什么?
编辑:堆栈跟踪如下:
java.lang.NullPointerException
at com.main.model.dao.JPAImpl.GenericDAOImpl.createNamedQuery(GenericDAOImpl.java:119)
at com.main.model.bo.DescriptorBO.persist(DescriptorBO.java:52)
at com.main.webmodule.JSONSerializer.deserialize(JSONSerializer.java:149)
at com.main.tests.TestModel.test(TestModel.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
这是persistence.xml:
Meta-INF/jpql/NamedQueries.xmlMysqL.jdbc.Driver"/>
MysqL://127.0.0.1:3306/testweb"/>
MysqL5Dialect"/>
最佳答案
我终于设法解决了这个问题.为了实例化GenericDAO,我使用了一个自动装配的注释,这样:
原文链接:https://www.f2er.com/spring/432569.html@Autowired
private GenericDAO genericDao;
但发生这种情况的类,称为DescriptorBO,以这种方式实例化:
DescriptorBO descrBO = new DescriptorBO(...);
因此完全逃脱了Spring容器的控制.将此更改为:
@Autowired
private DescriptorBO descrBO;
并将适当的bean定义添加到root-context.xml:
解决了这个问题.现在正确注入EntityManager.
获得的经验:如果Spring没有注入EntityManager(或任何其他注入的对象),请检查对象上方的所有对象层次结构是否由Spring管理,i.即直接或使用Autowired批注从应用程序上下文中的bean实例化.检查您是否使用new运算符来实例化这些对象!