19

I am using Gradle to build and test my projects. I have two projects:

ProjA  contains      src\test\java\BaseTest.java       
ProjB  contains      src\test\java\MyTest.java

MyTest extends BaseTest

When I run ProjB.gradle, how do I get it to see the BaseTest class from ProjA?

I tried adding:

dependencies {
  testCompile project('ProjA') 
}

But it didn't work.

Eusebius
  • 531
  • 6
  • 24
Britboy
  • 481
  • 2
  • 5
  • 10
  • 1
    Can you clarify "it didn't work"? Did you get an error message? Did your code not compile? Did you get a NoClassDefFound message during runtime? – Brandon McKenzie Aug 04 '15 at 18:36

1 Answers1

39

Maybe there are better, simpler ways, cleaner ways, but I think you have three options here.

First option

Since BaseTest is a class that is in fact part of a reusable testing library (you use it in both projects), you could simply create a testing subprojects, where BaseTest is defined in src/main/java and not src/test/java. The testCompile configuration of the other two subprojects would both have a dependency on project('testing').

Second option (UPDATED for Gradle 7.3)

In this second option, you would define an additional artifact and configuration in the first project:

configurations {
  testClasses {
    extendsFrom(testImplementation)
  }
}

task testJar(type: Jar) {
  archiveClassifier.set('test')
  from sourceSets.test.output
}

// add the jar generated by the testJar task to the testClasses dependency
artifacts {
  testClasses testJar
}

and you would depend on this configuration in the second project:

dependencies {
  testCompile project(path: ':ProjA', configuration: 'testClasses')
}

Third option

Basically the same as the second one, except it doesn't add a new configuration to the first project:

task testJar(type: Jar) {
  archiveClassifier.set('test')
  from sourceSets.test.output
}

artifacts {
  testRuntime testJar
}

and

dependencies {
  testCompile project(path: ':one', configuration: 'testRuntime')
}
afjord
  • 103
  • 5
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    I'm getting `Could not get unknown property 'testRuntime' for configuration ':projA:testClasses' of type org.gradle.api.internal.artifacts.configurations.DefaultConfiguration`. What am I missing here? – gopalanrc Mar 07 '18 at 03:58
  • Thanks, it still valid as today (2nd & 3rd options) – crgarridos Jul 10 '18 at 09:40
  • @gopalanrc I get the same error today but it's due to I'm working on a `com.android.library`. Have you resolved this ? – crgarridos Oct 09 '18 at 16:05
  • second option worked very good for me. – Garis M Suero May 14 '19 at 20:06
  • 2
    For Gradle 7+ you need to define a new configuration. So the second option is OK. – mike192 Jul 06 '21 at 12:45
  • For Gradle 7+ you need to change `extendsFrom(testRuntime)` to `extendsFrom(testRuntimeOnly)`. See [link](https://docs.gradle.org/current/userguide/upgrading_version_6.html#changes_7.0) for details. – afjord Feb 01 '22 at 07:34
  • Option #2 perfectly worked for me – vijaidhas Nov 23 '22 at 12:10
  • **Option #2 perfectly worked for me. details are below** **Added the below in project1** `task testJar(type: Jar) { archiveClassifier.set('test') from sourceSets.test.output } artifacts { testRuntime testJar } ` **Added below in project2:** `configurations { testImplementation.extendsFrom(testCompile) } dependencies { testCompile project(path: ':project1', configuration: 'testClasses') } ` – vijaidhas Nov 23 '22 at 12:38