Java 测试的渐变依赖项与编译的渐变依赖项不同
我在Android/Gradle依赖系统中面临这个问题Java 测试的渐变依赖项与编译的渐变依赖项不同,java,android,testing,gradle,dependencies,Java,Android,Testing,Gradle,Dependencies,我在Android/Gradle依赖系统中面临这个问题 我有一个相当复杂的JAR库,带有特定的API,比如other.JAR 这个other.sdk完全不受我的控制 此other.sdk仅适用于真正的手机 我需要编写要在Jenkins上执行的jUnit测试(不附带任何真实电话) 因此,我编写了一个名为say otherMock.JAR的JAR文件 此otherMock.jar与other.jar具有完全相同的API 现在,我需要在build.gradle文件中执行以下操作: ... depen
...
dependencies {
...
testCompile files('libs/otherMock.jar')
debugCompile files('libs/other.jar')
releaseCompile files('libs/other.jar')
...
}
...
不幸的是,Gradle似乎在我的测试构建中同时包含了other.jar和otherMock.jar
我确实明白,对于大多数构建(包括我以前的所有构建),这是完全可取的行为
但就我的特殊情况而言,这是一个问题
所以我需要(例如)这样的东西:
...
dependencies {
if(<I failed to realize which condition to write here to learn it is going to be a test>) {
testCompile ...
} else {
debugCompile ...
releaseCompile ...
}
}
。。。
依赖关系{
if(){
测试编译。。。
}否则{
调试编译。。。
发布编译。。。
}
}
或者更好:
...
dependencies {
testCompile ...
debugCompile ... {
exclude <when test build - what to write here?>
}
}
...
。。。
依赖关系{
测试编译。。。
调试编译{
排除
}
}
...
我尽了最大努力来实现如何配置Gradle来完成我在这里需要的工作,但我失败了:(
另一方面,我不能相信格雷德尔没有足够的灵活性来实现这一点。我希望这对我来说太难了:)
请不要给我以下建议:
- 在运行测试时包装other.jar并注入模拟的变体(我知道这是可能的,但这是大量额外工作,对我来说毫无意义)
- 将真正的手机连接到我的Jenkins(我无法做到这一点,因为我无法在真正的设备上访问Jenkins virtual machine+,我根本无法控制模拟)
- 应用其他模拟机制,如mockito-我也尝试了这个,但other.jar库没有遵循良好的设计原则,因此很难用标准方式模拟-我仍然没有尝试powermock,但如果我成功地处理依赖项,那么再次尝试对我来说毫无意义
configurations {
other.extendsFrom compile //latter is parent for both debugCompile and releaseCompile
}
test {
classpath -= configurations.other
}
dependencies {
testCompile files('libs/otherMock.jar')
other files('libs/other.jar')
}
对我来说,Android gradle插件不起作用。缺少测试任务。