Force JUnit gradle task to use the jar build in the project

I have a gradle project that generates a jar ( myFoo.jar ) in the build/libs folder. I have a few JUnit test that tests the classes from the myFoo.jar. The JUnits runs fine, except for the fact that when running the tests, the tests runs with bin/ in the classpath and the class loader is not really loading the build/libs/myFoo.jar. The tests instead run with bin/ in the classpath. So the JUnits passes except for a few.

One of my tests verifies some of the files under the resource folder in the jar, and these tests are failing with the ‘test’ task is run.

Is there any way for the gradle test task remove the bin/ from the classpath and instead include build/libs/myFoo.jar ?

Your clue is in the setClasspath method on the Test task type. You might need to do something like

test {
  classpath = project.files('/path/to/jar',configurations.runtime)

Thanks Schalk Cronjé. Your suggestions has worked. I’d included the following code to make it work.
classpath = project.files( "$buildDir/libs/"+jar.archiveName, "$buildDir/classes/test", configurations.runtime )