I have a small Spring web application, with the typical MVC Service DAO JPA/Hibernate Persistence Layer architecture. In production, I use a JTA-like persistence unit. The DAO is injected with an instance of the EntityManager via @PersistenceContext by the container. All is fine.
Now, I want to test my DAO implementations using an in-memory database (outside of a container on my local pc). I could manually create a RESOURCE_LOCAL based EntityManager. But how can I have it injected in my DAO implementations automatically?
I have seen this question and it suggests that it is possible with Spring. But how?
Of course, for unit testing, I could use new MyDAOImpl() and inject the EntityManager myself, but later, I will want to test my services which are injected with DAO implementations. I would like to avoid having to wire everything myself… Is this possible?
In our project, we define a different unit-testing-config.xml that has the datasource bean defined to point the in-memory database as follows:
The normal
entityManagerFactorydefintion as follows will use the abovedatasourcebean:And we run our
TestSuiteusing the following annotations:Hope this helps!