How to add to a test classpath?


(Noel Yap) #1

We have the following directory structure:

module/test/some.properties

module/test/com/company/SomeTest.java

When executing SomeTest, it’s not able to see some.properties. I’m assuming, but could be very wrong, that some.properties needs to be on the classpath. If this is correct, how can ‘"${projectDir}/test"’ be added to the classpath? The following emits an error saying something like the classpath cannot be changed:

test {

classpath.add(files("${projectDir}/test"))

}


(Noel Yap) #2

The following solves the problem:

test {

classpath = project.sourceSets.test.runtimeClasspath + files("${projectDir}/test")

}

I’m wondering if there’s a better way (ie a way that doesn’t rely on the knowledge that the default ‘classpath’ is ‘project.sourceSets.test.runtimeClasspath’).


(Luke Daley) #3

You want:

sourceSets {
  test {
    resources {
      srcDir "test"
    }
  }
}

(Giovanni Botta) #4

Hey Luke I have been using what you suggested above to include a specific folder from a different module as a test resource folder, e.g.,

sourceSets.test.resources.srcDir project(’:common’).file(‘config’)

I have that line in two different modules, say ‘x’ and ‘y’.

I am also using the idea plugin to generate intellij configuration files. Then when I go in the IntelliJ Project Structure (Ctrl + Alt + Shift + S) and try to manually modify a module and then hit apply, I get the error

Module “x”

must not contain source root “/path/to/project/common/config”.

The root already belongs to module “y”

I believe this is some weirdness with IntelliJ. Is there a way to address it from Gradle idea?


(Luke Daley) #5

The only way to address is to remove the need to make manual modifications in IntelliJ. IntelliJ just doesn’t allow source outside of the “source root” for a module.

Why do you need to make manual modifications.


(Giovanni Botta) #6

Unfortunately I’m dealing with a legacy project with quirky class path choices and modules structure. I am trying not to modify the structure too much but this might have to be changed because I don’t see a solution. Thanks


(Luke Daley) #7

What OS are you on? If it’s a *NIX, symlinks might be an option here.


(Giovanni Botta) #8

I fixed it as per this SO