简体   繁体   中英

Classpath problems when running JUnit tests from Eclipse with Gradle build

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?

It seems that you are abusing the class path to pass a single argument (the absolute file path of the XML file) to a test. Instead, you should either put the XML file on the (test) class path in the correct way (it needs to go into a directory or Jar file that's listed on the class path) and load it correctly (eg with getClass().getClassLoader().getResource("some/resource.xml") ), or pass the file path to the test as a system property. Naturally, the latter will be harder to make work for different environments (say Gradle build and IDEs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM