zyxist / chainsaw

Gradle plugin: adds support for building Java 9 modules.
Apache License 2.0
70 stars 4 forks source link

test resources not available to unit tests #33

Open SingingBush opened 6 years ago

SingingBush commented 6 years ago

I've got exports my.package to junit; in my module-info and most of my tests can now run while building but some tests rely on test data that I place in src/test/resources. when doing a build these tests fail as the resources are not found. I've tried various ways to get the resources such as:

InputStream stream = getClass().getModule().getResourceAsStream("testdata.txt");
InputStream stream = getClass().getClassLoader().getResourceAsStream("testdata.txt");
SingingBush commented 6 years ago

for now I have a workaround that works when running the tests in Intellij as well as when the tests are run during the gradle build:

    private Reader loadTestResource(final String resource) throws IOException {
        final InputStream stream = ClassLoader.getSystemResourceAsStream(resource);

        return stream != null ?
                new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8"))) :
                Files.newBufferedReader(Paths.get("src/test/resources/" + resource));
    }
udaychandra commented 6 years ago

Yes, the test resources aren't being picked up by the modular setup. I tried modifying the test source set output directory for resources and it seems to be working:

sourceSets {
    test {
        output.resourcesDir = "build/classes/java/test"
    }
}