Setting Datasource in arquillian

967 Views Asked by At

So, I have a project which uses persistence.xml in main, it is in src/main/resources/META-INF

Now i want to use an in-memory database (H2) in writing automated unit test so i need to have another persistence.xml, i have it in src/test/resouce/META-INF

So my question is how can i over-ride the first persistence,

I am also using shrikwrap resolver to resolve all dependencies from pom.xml, so it resolves it all and i add it to shrikwrap to return WebArchive, is there any way, i tried many such as creating profile or maven filetering but doesnt work with Arquillian as i resolve everything and add to deployment.

You can find my Arquillian deployment below :

    WebArchive[] file = Maven.resolver().loadPomFromFile("pom.xml")
            .resolve("G:A:V").withTransitivity().as(WebArchive.class);

    WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
            .addAsResource("META-INF/persistence.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

    war.addAsLibraries(file);

    return war;
2

There are 2 best solutions below

0
On

Have you tried adding the new resource after addAsLibrary(file)?

    WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
            .addAsLibraries(file)
            .addAsResource("META-INF/persistence.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
0
On

You have at least two options here.

  • Create it directly in the code using StringAsset - addAsResource(new StringAsset("<xml content>"), "META-INF/persistence.xml")
  • Keep your h2 file under different name, e.g. h2-test-persistence.xml, in src/test/resources/ and add it using addAsResource("h2-test-persistence.xml", "META-INF/persistence.xml")