5

我是 jpa 和 spring 世界的新手,我目前正在对一个简单的方法进行一些单元测试,但只有当我在单元测试模式下运行我的测试类时才会收到此错误消息:

java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
at $Proxy19.unwrap(Unknown Source)
at com.gemstone.integration.PersonDao.getPersonByUserNamePassword(PersonDao.java:59)
at com.gemstone.integration.PersonDaoTest.getPersonByUserNamePassword_Exist(PersonDaoTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
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)

但是,当我运行我的项目时,代码工作正常,它可以正确检索数据(不在单元测试模式下)。

请在下面找到我的单元测试将基于的方法:

@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
public boolean getPersonByUserNamePassword(String firsName, String password) {

    String hql = "from Person p where p.firstName = :firsName and p.password =:password";
    Session mysession = entityManager.unwrap(Session.class);

    Query query = mysession.createQuery(hql);
    query.setParameter("firsName", firsName);
    query.setParameter("password", password);
    List<Person> results = query.list();

    if (results != null && results.size() > 0) {

        return true;
    }
    return false;

}

我的单元测试如下:

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:/Intcontext.xml" })

public class PersonDaoTest {

    @Autowired
    PersonDao personDao;

    @Transactional
    @Test
    public void getPersonByUserNamePassword_Exist() {

        String firsName = "Andy";
        String password = "123";

        boolean isUserExists = personDao.getPersonByUserNamePassword(firsName,
                password);

        Assert.assertTrue(isUserExists);
    }

}

有什么想法为什么我会收到 No transactional EntityManager available 错误吗?:(

提前致谢。

4

3 回答 3

5

通过声明

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })

您完全(尽管是无意的)禁用了对测试管理事务的支持。原因是您TransactionalTestExecutionListener从声明的侦听器列表中省略了。

但是请注意,TransactionalTestExecutionListener默认情况下它是透明地声明的。@TestExecutionListeners因此,当您从测试类中删除您的声明时,TransactionalTestExecutionListener它会再次启用。

您当然可以在此处的 Spring 参考手册中找到详细信息。

问候,

Sam(Spring TestContext 框架的作者;))

于 2013-01-23T14:32:36.153 回答
2

I think EntityManager is null.Try Like this,may be it work

private EntityManager entityManager = null;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}
于 2013-01-23T07:53:54.200 回答
0

当我尝试使用 IntelliJ 进行单元测试时,我遇到了类似的问题,即使它cli使用maven-surefire-plugin

事实证明,IntelliJ 构建使用的是 javac 编译器,即使它在 pox.xml 中指定使用 ajc 编译器,在IDEA-135483处报告了 IntelliJ 的错误

这使得@Transactional不工作,给你No transactional EntityManager available

如果这是您的情况,您需要更改 java 编译器以在项目结构中使用而不是ajc使用javacpost-compile weave mode

于 2020-04-13T17:45:53.213 回答