Classpath problems when running JUnit tests from Eclipse

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Classpath problems when running JUnit tests from Eclipse

tbain98
I have a Gradle project that declares a test-only dependency on an XML data file, and then loads the file from the classpath.  When I run the tests directly in Gradle from the command line, everything works fine, but when I run "gradlew eclipse", refresh the project in Eclipse, and then try running the test from Eclipse (Debug As -> JUnit Test), the test fails because it's unable to find the XML file and the classpath (as accessed from the Properties context menu item on the process in the Debug view) shows no indication of the XML file being included on the classpath.

The behavior I'm seeing has some commonality with http://gradle.1045684.n5.nabble.com/gradle-junit-tests-resources-and-classpath-td4418753.html#a4420758, but Sean's problem was the reverse: his tests ran properly under Ant (but he never mentioned trying to run directly from the Eclipse JUnit plugin), but not under Gradle.

Here's the relevant part of build.gradle:
dependencies {
    testCompile group: 'com.mycompany', name: 'MyConfigFile', version: '0.0.0+dirty', ext: 'xml'
}

Because the only resources that URLClasspathLoader can load directly from the file system are JARs, I'm using the following static method to search the classpath for files that match the filename I need to load:
    public static String getFullPathForResourceDirectlyOnClasspath(String nameFragment) {
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        for (URL url: ((URLClassLoader)cl).getURLs()){
            String fullPath = url.getFile();
            if (fullPath.contains(nameFragment)) {
                return fullPath;
            }
        }
        return null;
    }

I call that method as follows:
getFullPathForResourceDirectlyOnClasspath("/MyConfigFile-");

When I run that code from Gradle ("gradlew build"), it finds the file and my test succeeds.  When I run it from Eclipse (Debug As -> JUnit Test), it fails to find that file on the classpath (because the Eclipse JUnit plugin doesn't put it there) and that call returns null.

I've tried changing the configuation from testCompile to compile to see if that made a difference, but it doesn't change anything (and perhaps tellingly, my .classpath doesn't have any entry for the XML file even when the compile configuration is selected).

Does anyone know of a way to make this work?  Am I just missing something that should be obvious?