javapathjboss-arquillianshrinkwrap

Arquillian: path relative to project instead of server


How can I use a project-relative path when using Arquillian/Shrinkwrap to test my testcases?

IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File("src\\ZZZZZ.xml"));
insertFromXML(dataSet);

Will give me this exception when testing
java.io.FileNotFoundException: C:\Uprogs\jboss-eap-6.2.4\bin\src\ZZZZZ.xml

It tries to locate the file within the folders of the server on which I deploy the test onto. Rather than that, I want him to look in the folders relative to my project (e.g. C:\Users\xy\workspaces\MyProject\src\ZZZZZ.xml). Searched the internet but found nothing

Shrinkwrap gets deployed like this:

@Deployment
public static Archive<?> createDeployment() {
    File[] libs = Maven.resolver()  
            .loadPomFromFile("pom.xml").resolve(
                    "jcifs:jcifs"
                    , "org.dbunit:dbunit"
                    , "com.ibm:db2jcc_license_cisuz"
                    , "com.ibm:db2jcc"
                    )  
            .withTransitivity()
            .asFile();  

    return ShrinkWrap.create(WebArchive.class, "test.war")
            .addAsLibraries(libs)
            .addPackage("de.abc.RuleEditor")
            .addAsResource("de/abc/RuleEditor/messages.properties", "messages.properties")
            .addAsManifestResource("test-jboss-deployment-structure.xml","jboss-deployment-structure.xml")
            .addAsWebInfResource("test-beans.xml", "beans.xml")     
            .addAsWebInfResource(
                    new StringAsset("<faces-config version=\"2.0\"/>"), "faces-config.xml")
                    .merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class)  
                            .importDirectory("src/main/webapp").as(GenericArchive.class), "/", Filters.include(".*\\.xhtml$"));
}

Solution

  • How can I use a project-relative path when using Arquillian/Shrinkwrap (...)?

    It's a ridiculous approach, don't go that way :]

    The idea behind Arquillian is to create micro-deployments (it means: jar/war/ear archive using ShrinkWrap tool) and include everything inside that archive.

    So please modify your deployment:

    @Deployment
    public static Archive<?> createDeployment() {
    
    return ShrinkWrap.create(WebArchive.class, "test.war")
            (...)
            .addAsLibraries(libs)
            // Add in below way any additional file to the archive
            .addAsResource("path-to-testset.xml", "dbunit/testset.xml")
            .addPackage("de.abc.RuleEditor")
            .addAsResource("de/abc/RuleEditor/messages.properties", "messages.properties")
            .addAsManifestResource("test-jboss-deployment-structure.xml","jboss-deployment-structure.xml")
            (...)
    }
    

    And then use getResourceAsStream() to load file from the class path:

    @Test
    public void test_something() {
       FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
       builder.setColumnSensing(true);
    
       FlatXmlDataSet xmlDataSet = builder.build(
                this.getClass().getResourceAsStream("/dbunit/testset.xml"));
      // ...
    

    It is much better, as everything what is needed to test, is already included inside archive. No relative, nor absolute path names.

    My personal advice is: be careful about DbUnit, as perhaps you notice in the future, crafting and managing many xml data sets may become bigger and bigger problem as your project will grow. That is why I prefer DbSetup.