在单元测试中通过Gradle排除罐子 [英] Excluding jars via Gradle in unit tests
问题描述
我要使用 fileTree()
包含另一个项目中的一些本地构建的库:
I'm including some locally-built libs from another project by using fileTree()
:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
}
对于单元测试,我想使用自己的模拟类而不是这些jar.我如何才能 testImplementation
配置不不使用那些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?
推荐答案
在我的情况下,我需要包括aar来实现,并用jar替换它以进行单元测试.Gradle 无法排除jar文件,所以我发现了不同之处解决方案.
In my case I needed to include aar for implementation and replace it with jar for unit tests. Gradle can't exclude jar files, so I found different solution.
假设我的文件夹 MyProject
中有一个Android项目.因此,必须有文件 MyProject/build.gradle
和 MyProject/app/build.gradle
.我将< my-dependency> .aar
文件和< my-test-dependency> .jar
文件放入 MyProject/app/libs
目录.然后,将该目录作为本地存储库添加到 MyProject/build.gradle
文件中:
Let's say I have Android project in folder MyProject
. So there must be files MyProject/build.gradle
and MyProject/app/build.gradle
.
I put <my-dependency>.aar
file and <my-test-dependency>.jar
files to MyProject/app/libs
directory. Then I add this directory as local repository in MyProject/build.gradle
file:
allprojects {
repositories {
...
flatDir {
dirs 'libs'
}
}
}
现在我可以按名称包括和排除我的aar:
Now I can include and exclude my aar by name:
configurations.testImplementation {
exclude module: '<my-dependency>'
}
dependencies {
implementation(name: '<my-dependency>', ext:'aar')
testImplementation(name: '<my-test-dependency>', ext:'jar')
// fileTree also should work, i.e.:
// testImplementation fileTree(dir: 'libs', include: ['*.jar'])
}
这篇关于在单元测试中通过Gradle排除罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!