I need to create a unit test with a non-empty beans.xml or persistence.xml using Arquillian. How do I do that?
The simplest way is to have a test version of your persistence.xml or beans.xml stored as a separate file in your source tree, and have ShrinkWrap add it to your test archives as a file asset, eg:
return ShrinkWrap.create(JavaArchive.class) .addAsManifestResource(new FileAsset(new File("src/test/resources/META-INF/test-persistence.xml")), "persistence.xml"); ....
Sometimes that's not enough, though. Maybe your persistence.xml (or other descriptor) is complicated and it's a pain to maintain two copies. Maybe you just want to make sure your tests exactly reflect your real environment, so you don't forget to change both descriptors. Or maybe different tests need different descriptors and it'd quickly get painful to maintain them all.
That's what the ShrinkWrap descriptors extension is for. It provides models for the various Java EE descriptors and API for loading or creating them, modifying them, and writing them out to strings ready for inclusion in ShrinkWrap archives.
The ShrinkWrap Descriptors 2.0.x alpha releases change the API significantly over 1.x, and add a lot more functionality, so I suggest you go straight to the 2.x alphas. Arquillian 1.0.0.Final's bom specifies descriptors 2.0.0-alpha-2, so that's what you'll get if you don't say otherwise.
To use ShrinkWrap descriptors, first add it to your dependencies in your pom.xml:
<dependencies> .... <dependency> <groupId>org.jboss.shrinkwrap.descriptors</groupId> <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId> <scope>test</scope> </dependency> </dependencies>
... then use the org.jboss.shrinkwrap.descriptor.api.Descriptors entry point. Here's an example showing how to create a beans.xml with an alternative and an interceptor, and a persistence.xml with a JTA datasource and name:
BeansDescriptor beans = Descriptors.create(BeansDescriptor.class) .createAlternatives().clazz("com.example.AlternativeBean").up() .createInterceptors().clazz("org.jboss.seam.security.SecurityInterceptor").up(); PersistenceDescriptor persistence = Descriptors.create(PersistenceDescriptor.class) .createPersistenceUnit() .name("test-PU") .jtaDataSource("java:jboss/datasources/ExampleDS") .up(); return ShrinkWrap.create(JavaArchive.class) .addAsManifestResource(new StringAsset(persistence.exportAsString()), "persistence.xml") .addAsManifestResource(new StringAsset(beans.exportAsString()), "beans.xml") ....
It is important to note that the locations of beans.xml and persistence.xml vary depending on the archive type and ShrinkWrap cannot currently take care of this for you automatically. For a WebArchive, you'd instead need:
return ShrinkWrap.create(WebArchive.class) .addAsResource(new StringAsset(persistence.exportAsString()), "META-INF/persistence.xml") .addAsWebInfResource(new StringAsset(beans.exportAsString()), "beans.xml") ....
You also need to be aware that at time of writing the descriptors module is still in alpha, and has known bugs. Loading and modifying existing descriptors seems to be more issue-prone than creating them afresh, so keep that in mind.
Comments