我正在将我的代码库升级到Gradle 2.2和Android Studio 1.0。我目前正在尝试让Robolectrial2.4正常工作,但我在尝试运行单元测试时遇到了一个奇怪的问题。该问题仅在gradle clean
之后发生;多次运行测试套件将产生通过测试的测试(正如预期的那样)。当我在清理后运行测试时,我得到以下错误:
java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException
我已经将这个错误追溯到这个调用:
Activity activity = Robolectric.setupActivity(MainActivity.class);
无论我使用的是Robolectric gradle插件(org.robolectric:robolectric-gradle-plugin:0.14.0
)还是JC和K单元测试插件(com.github.jcandksolutions.gradle:android-unit-test:2.1.1
),我都会遇到这个错误。
我在Robolectric Github上发现了这个问题,但它看起来还没有得到解决:https://github.com/robolectric/robolectric/issues/1385
这个问题也在android studio单元测试插件的'Trouble‘下被引用:https://github.com/evant/android-studio-unit-test-plugin
我当前的示例代码是:https://github.com/KioKrofovitch/robolectric-upgrade-test
虽然api-android-19和api-android-21项目由于其他原因而失败,但我已经能够在Robolectric示例上运行api-android-16项目而没有看到这个问题。我看不出他们做了什么不同的事情,以至于他们没有得到这个失败。https://github.com/robolectric/robolectric-samples
有没有人找到解决这个问题的办法?对于像Jenkins或Travis这样的CI工具来说,运行两次测试并不是一个好的解决方法。
编辑:嵌入代码示例
我的顶层build.gradle,我添加了JC和K单元测试库:
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
我的项目级build.gradle,我在其中添加了robolectric:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.robolectrictest"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
androidTest {
setRoot('src/androidTest')
}
}
}
// Must be after Android plugin
apply plugin: 'android-unit-test'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
//androidTestCompile 'junit:junit:4.10'
//androidTestCompile 'org.robolectric:robolectric:2.4'
// Testing frameworks
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.4'
}
我最基本的活动,一切都是模板创建的:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的虚拟测试,由于setupActivity()方法错误,我无法访问assert:
@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class ApplicationTest {
@org.junit.Test
public void testDummy() throws Exception {
Activity activity = Robolectric.setupActivity(MainActivity.class);
assertTrue(true);
}
}
编辑#2:为了运行测试,我从项目的顶级目录调用以下命令:
./gradlew clean
./gradlew test
我还尝试在gradle包装器之外运行测试,并获得相同的结果
gradle clean
gradle test
发布于 2015-03-27 16:07:25
在Android Gradle插件从1.1.0升级到1.1.3之后,这个例外就消失了。没人能证实这一点吗?我正在使用这里描述的设置:http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html
发布于 2015-01-07 15:24:48
我的问题解决了。我将我的robolectirc更新为2.4,将最小SDK更新为19,并将android支持更新为com.android.support: support -v13:19.1.0。并对iml文件进行了同样的更改。
感谢您的指导和回复
Venkatraman
https://stackoverflow.com/questions/27574165
复制相似问题