Transactional EntityManager not available

I am new to jpa and spring world and I am currently doing some unit test by simple method but keep getting this error message only when running my test class in unit test mode:

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)

      

However, the code works fine, when I run my project, it fetches data correctly (not in unit test mode).

Below is my method which unit test will be based on:

@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;

}

      

My unit test is below:

@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);
    }

}

      

Any ideas why I am getting No transactional EntityManager error available please ?:(

early.

+3


source to share


2 answers


By announcing

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })

      

you have (albeit unintentionally) disabled support for check-driven transactions. The reason is that you omitted TransactionalTestExecutionListener

from the list of declared listeners.

Note, however, that it is TransactionalTestExecutionListener

declared transparent by default. This way, when you remove the declaration @TestExecutionListeners

from your test class, TransactionalTestExecutionListener

it is included again.



You can of course find it in the Spring reference manual here .

Hello,

Sam (author of Spring TestContext Framework;))

+5


source


I think EntityManager is null.Try Like this maybe it works



private EntityManager entityManager = null;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

      

+2


source







All Articles