简体   繁体   中英

Excluding jars via Gradle in unit tests

I'm including some locally-built libs from another project by using fileTree() :

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...
}

For unit testing, I want to use my own mock classes instead of those jars. How can I have the testImplementation configure not use those jar files and instead use similarly-named classes out of my source hierarchy?

By default the testImplementation configuration extends from the implementation one so every dependency added to implementation will be present in testImplementation .

So the best option is to declare these specific dependencies into a different configuration, let's call it extraDeps , which you then add to the compileClasspath configuration:

configurations {
    extraDeps
    compileClasspath.extendsFrom(extraDeps)
}

dependencies {
    extraDeps fileTree(dir: 'libs', include: ['*.jar'])
}

This gives you the following advantages:

  • Shared dependencies between compile and test can still be in implementation
  • Special dependencies are identified clearly as they are in their own configuration
  • Compile classpath sees all it needs
  • Test classpath does not see the special jars

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