我的Android Studio项目从未完成命令行编译。所有编译均已使用IDE完成。我试图在持续集成服务器上引入脚本编译,但是遇到了麻烦:我的build.gradle如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.foo.myapplication"
minSdkVersion 18
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
}
dependencies {
compile project(':library')
compile project(':gdHandheld')
testCompile 'org.jmockit:jmockit:1.28'
testCompile 'junit:junit:4.12'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
当我通过android studio运行时,我的测试运行良好。我将jmockit用作这样的测试框架:
@Test
public void onOptionsItemSelectedRequestHelpTest(@Mocked final MenuItem menuItem,
@Mocked final RequestHelpDialogFragment requestHelpDialogFragment)
{
Assert.assertTrue(true);
}
但是,当我从命令行运行gradlew check
时(使用gradle-2.14.1-all),则我认为类路径在某种程度上是不正确的。它引发错误
java.lang.Exception: Method onOptionsItemSelectedRequestHelpTest should have no parameters
我有理由确定,问题是订购之一。运行单元测试时,jmockit必须出现在类路径上的junit之前。 gradlew --debug
几乎可以确认顺序是相反的。我需要做些什么才能使jmockit正确地允许junit对测试进行参数处理?顺便说一句,一旦我通过了测试(可能是通过jacoco),我也希望能够对此进行代码覆盖。我提到这一点是因为我不想拥有一种会破坏jacoco兼容性的解决方案
我在使用junit 4.12和jmockit 1.46时遇到了完全相同的问题。我看到了在gradle配置中添加以下内容的建议,
test {
jvmArgs "-javaagent:${classpath.find {it.name.contains("jmockit") }.absolutePath}"
}
但是这导致以下错误,
Cannot change dependencies of configuration ':testCompile' after it has been included in dependency resolution
我最终能够通过升级到jmockit 1.8版来解决原始问题