4

I am trying to add a resource to an Arquillian shrink-wrap archive and to load it via the classpath. However, I did not manage to make it working. I tried using addAsResource() and addAsWebInfResource(). The version below is the one I think should work, but does not. Any solutions?

import static org.junit.Assert.assertNotNull;

import java.io.InputStream;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class ResourceLoadingTest {

    @Deployment
    public static WebArchive createDeployment() {
        WebArchive archive = ShrinkWrap.create(WebArchive.class)
                .addAsWebInfResource(new StringAsset("Hello World"),
                        "classes/test.properties");
        System.out.println(archive.toString(true));
        return archive;

    }

    @Test
    public void testLoadResource() {
        InputStream stream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("/test.properties");
        assertNotNull(stream);
    }
}

Console output:

411ad6b1-7705-406f-ba05-d7f46ca14966.war:
/WEB-INF/
/WEB-INF/classes/
/WEB-INF/classes/test.properties
ruediste
  • 2,434
  • 1
  • 21
  • 30
  • Have you maybe tried already: InputStream stream = MyClass.class.getResourceAsStream("classes/test.properties") where MyClass is a class in the classes directory? – dbalakirev May 22 '14 at 11:37
  • Or maybe this: this.class.getClassLoader().getResourceAsStream("test.properties") – alex Jul 10 '14 at 11:12
  • 1
    This indeed should work, I think your problem might be related to this issue: http://stackoverflow.com/questions/3263560/sysloader-getresource-problem-in-java Check if you by any chance don't have spaces in your full path to that resource (including server location). – JanM Sep 10 '14 at 15:55

0 Answers0