I have a problem inside the business code of our JAVA EE application server.
We access some files inside the src/main/resources
folder with
InputStream inputStream = Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream(filePath);
In production this works, but I would like to test a part of code which use this functionality inside my arquillian test.
We are using shrinkwrap to generate our test.war
.
WebArchive testArchive = ShrinkWrap.create(WebArchive.class, "test.war")
.addPackages(true, "ch.microtronic.evending")
.addAsWebInfResource("wildfly-ds.xml")
.setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))
.addAsResource("test-persistence.xml", "META-INF/persistence.xml");
I have some trouble to add the resources from src/main/resources
to the test.war
.
I can only add files from src/test/resources
.
Our directory structure looks like:
src
|__main
| |__java
| |__resources
| |__webapp
|
|__test
|__java
|__resources
What I have to do?
If you want to add files that are not in src/test/resources
you just need to use an overloaded version addAsResource(File, String)
instead of addAsResource(String, String)
. For your example that would look like:
.addAsResource(new File("src/main/resources/test-persistence.xml"),"META-INF/persistence.xml");
I've just solved similar issue in my code, so this approach definitely works.
This one-liner adds recursive all main resources from src:
testArchive.addAsResource(new File("src/main/resources/"), "");
this one for some resources postprocessed by maven:
testArchive.addAsResource(new File("target/classes/META-INF/"), "META-INF/");
To include a subtree I use the following.
Say that you need in your classpath all files under src/main/resources/db/migration
as db/migration/FILE_NAME
Path resources_dir = Paths.get("src", "main", "resources");
Path migrations_dir = resources_dir.resolve(Paths.get("db", "migration"));
Files.walk(migrations_dir)
.forEach((p) -> jar.addAsResource(p.toFile(),
resources_dir.relativize(p).toString()));
relativize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With