1

While running JUnit integration tests inside Eclipse, I have two persistence.xml files on the classpath - one containing the configuration for the test cases and another one for production. I need to find a way to exclude the persistence.xml for production from the test classpath.

Constraints: I am not allowed to use Maven or Ant, just Eclipse Helios SR1 (with JUnit). The JPA 2 persistence provider is EclipseLink 2.2.0. As I have to use ClearCase 7.0 as VCS, no linked resources are possible, as described here, for example.

Here is the rough project structure:

project-datamodel (containing the Entity beans)
\- src/resources/META-INF/persistence.xml (for production; want to exclude it)
\- test/resources/META-INF/persistence.xml (for test)

project-service (containing the Session beans)
\- ejbModule

project-service-it (containing the JUnit integration tests for the Session beans)
\- test
Community
  • 1
  • 1
Bastian
  • 11
  • 1
  • 3

2 Answers2

1

I had this problem because my two persistence.xml files (production and test) used the same <persistence-unit> name. As mentioned above, the classpath order does not matter as EclipseLink apparently finds all persistence.xml files before deciding which to use. I solved it by renaming the test PU in both the xml file and the call from my unit test:

@BeforeClass
public static void setup() {
    em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
}
Matthew Cornell
  • 4,114
  • 3
  • 27
  • 40
0

You can edit the classpath when you create a run configuration for your unit tests (Eclipse will create one automatically if you pick "Run As -> Junit Test" from the context menu.

When editing that new run configuration you can add custom folders on the "Classpath" tab and move them up in front of the default classpath. I have not tried it, but that should do it, because in the test it would find the test persistence.xml first.

Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72
  • 1
    Thank you Daniel for your answer! The problem is not the classpath order, as I got it correct, but the following: As one can have more persistence units, EclipseLink processes all persistence.xml files in the classpath. In my case, the persistence unit is the same in both persistence.xml files. I do not know EclipseLink, but it seems to merge/overwrite the configuration from both files resulting in annoying errors. That is why I simply want to exclude the persistence.xml for production. – Bastian Jul 28 '11 at 09:50
  • My previous assumption was wrong, of course. With the current project structure, there can not be two persistence.xml files on the classpath, as Eclipse simply overwrites the first one (production) with the second one (test) by copying it to the target directory. That is, because the src/resources directory is listed before the test/resources directory. So with this structure, I should have the vice-versa problem: The configuration for production is no longer possible. – Bastian Jul 28 '11 at 10:30
  • I moved the persistence.xml files as follows: **project-service:** `ejbModule/META-INF/persistence.xml` (for production) **project-service-it:** `test/resources/META-INF/persistence.xml` (for test) Now, I am back at the beginning... The classpath ordering does nothing change :(. – Bastian Jul 28 '11 at 12:01