JSBach
JSBach

Reputation: 4747

How can I generate a persistence.xml tailored for each of my arquillian tests?

I am learning Arquillian to see if I can add to it project.

I was able to create a test and deploying a "static" test-persistence.xml, similar to this tutorial.

My problem is that my real persistence.xml has different mappings () and entities (). To get my tests to work I have to either:

  1. Add all classes and mappings to the persistence.xml, which will cause every test to have to insert all entities and mappings to the package and would add cost of maintenance to the test-persistence.xml/tests when new entities/mappings are added to the system
  2. Somehow add the mappings/entities on-the-fly to my persistence unit
  3. Pre-generate the persistence.xml before creating the package to deploy
  4. Have one persistence.xml for each TestCase

Alternative 4 is not feasible, since it can't be maintained. Alternative 1 is feasible, but I would like to keep my builds smaller. I tried both 2 and 3, but I was not able to find any way (I am using JavaEE, JPA 2.0 and Hibernate) to make any of them. It seems that Hibernate does not support adding entities/mappings on-the-fly and that Archive.AddAsResource accepts just a path, not a "file content", or something like this.

I think this is a common issue when testing with Arquillian. How can I build my persistence.xml in a way that it has only the things required for my test?

Upvotes: 2

Views: 149

Answers (1)

AGdev
AGdev

Reputation: 616

Create a test version of your persistence.xml stored as a separate file in your source tree, and have ShrinkWrap add it to your test archives as a file asset.

return ShrinkWrap.create(JavaArchive.class)  
             .addAsManifestResource(new FileAsset(new File("src/test/resources/META-INF/test-persistence.xml")), "persistence.xml");

Upvotes: 2

Related Questions