Reputation: 177
We are working with gradle and have the default project structure:
/src
/main
/java
/resources
/META-INF
persistence.xml
/test
/java
/resources
/META-INF
persistence.xml
While we run our junit tests with gradle, all is fine (we do not have any special gradle configuration concerning source paths or the like): the persistence.xml from 'test' is used.
But if we try to run our junit tests with eclipse, always the peristence.xml from 'main' is used.
How can we configure eclipse to use the persistence.xml from 'main' for one launch configuration, but the persistence.xml from 'test' for all junit tests?
Upvotes: 0
Views: 1682
Reputation: 18780
Another thing to try, if this is a general problem about resources loaded from your classpath:
In Eclipse's project properties, Java Build Path->Order and Export, put your test folder above your src folder.
Then under Deployment Assembly, exclude your test folder
That will ensure that when you launch tests directly from within Eclipse, you will search the test folder first for classpath resources. But when you launch as a webapp instead, the test folders would be excluded from WEB-INF/classes
.
Upvotes: 1
Reputation: 18780
I've solved this problem with a single persistence.xml and different persistence units.
<persistence-unit name="MAIN">
...
</persistence-unit>
<persistence-unit app="TEST">
...
</persistence-unit>
Then in your app, you would inject the main persistence unit with
@PersistenceContext(name="MAIN", unitName="MAIN")
private EntityManager entityManager;
And for unit tests, you would explicitly create an EntityManager using the TEST
persistence unit, and inject it manually into the object under test:
EntityManager em = Persistence.createEntityManagerFactory("TEST").createEntityManager();
bean.setEntityManager(em);
Upvotes: 0