ClassCastException异常:出现NoClassDefFoundError不能转换为RuntimeException的 [英] ClassCastException: NoClassDefFoundError cannot be cast to RuntimeException

查看:238
本文介绍了ClassCastException异常:出现NoClassDefFoundError不能转换为RuntimeException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在升级我的codeBase类到摇篮2.2和Android工作室1.0。目前我正在试图让Robolectric 2.4的工作,但我遇到一个奇怪的问题,当我尝试和运行单元测试。出现该问题后,才摇篮干净;运行测试套件多次将产生通过测试(如预期)。当我经过一个干净的运行测试,我得到以下错误:

  java.lang.ClassCastException:java.lang.NoClassDefFoundError的不能转换到了java.lang.RuntimeException
 

我已经追查错误回到这个电话:

 活动活动= Robolectric.setupActivity(MainActivity.class);
 

我遇到这个错误我是否使用Robolectric摇篮插件( org.robolectric:robolectric-摇篮 - 插件:0.14.0 )或JC和K Android的单元测试插件( com.github.jcandksolutions.gradle:Android的单元测试:2.1.1 )。

我发现这个问题上的Robolectric Github上引用的,但它并不像它已被尚未解决: <一href="https://github.com/robolectric/robolectric/issues/1385">https://github.com/robolectric/robolectric/issues/1385

这问题也被引用在Android工作室的单元测试插件,故障排除项下: <一href="https://github.com/evant/android-studio-unit-test-plugin">https://github.com/evant/android-studio-unit-test-plugin

我目前的样品code是在这里: <一href="https://github.com/KioKrofovitch/robolectric-upgrade-test">https://github.com/KioKrofovitch/robolectric-upgrade-test

我已经能够没有看到这个问题,运行的API,Android为16的项目上Robolectric样本,尽管API-的Andr​​oid-19和API,Android为21个项目失败的其他原因。我看不出他们在做什么不同,使得他们没有得到这个故障。 <一href="https://github.com/robolectric/robolectric-samples">https://github.com/robolectric/robolectric-samples

有没有人找到了一个解决方法吗?运行测试两次,是不是一个很好的办法解决像詹金斯或特拉维斯CI工具。

编辑:嵌入code样品

我的顶级build.gradle,我添加了JC和K单元测试库:

  buildscript {
    库{
        mavenCentral()
        jcenter()
    }
    依赖{
        类路径com.android.tools.build:gradle:1.0.0
        类路径com.github.jcandksolutions.gradle:Android的单元测试:2.1.1

        //注意:不要在这里放置应用程序依赖;他们属于
        //在单个模块build.gradle文件
    }
}

allprojects {
    库{
        jcenter()
    }
}
 

我的项目水平build.gradle,在这里我想补充的robolectric:

 应用插件:com.android.application

库{
    mavenCentral()
}

安卓{
    compileSdkVersion 21
    buildToolsVersion21.1.2

    defaultConfig {
        的applicationIDcom.example.robolectrictest
        的minSdkVersion 15
        targetSdkVersion 21
        版本code 1
        VERSIONNAME1.0
    }
    buildTypes {
        推出 {
            minifyEnabled假
            proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'proguard-rules.pro
        }
    }

    sourceSets {
        主要 {
            jniLibs.srcDirs = ['库']
        }

        androidTest {
            setRoot(SRC / androidTest)
        }
    }
}

//必须是Android插件后,
应用插件:Android的单元测试

依赖{
    编译文件树(导演:库,包括:['的* .jar'])
    编译com.android.support:appcompat-v7:21.0.3

    // androidTestCompile'的JUnit:JUnit的:4.10
    // androidTestCompileorg.robolectric:robolectric:2.4

    //测试框架
    testCompile'的JUnit:JUnit的:4.10
    testCompileorg.robolectric:robolectric:2.4
}
 

我很基本的活动,一切都已经离开作为模板创建它:

 公共类MainActivity扩展ActionBarActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }


    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。将操作栏
        //自动在主/向上按钮操作的点击,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT的id = item.getItemId();

        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }

        返回super.onOptionsItemSelected(项目);
    }
}
 

我的假人测试中,我不能让我的断言,由于setupActivity()方法错误:

  @Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)

公共类ApplicationTest {

    @ org.junit.Test
    公共无效testDummy()抛出异常{

        活动活性= Robolectric.setupActivity(MainActivity.class);

        assertTrue(真正的);
    }

}
 

编辑#2: 要运行测试,我从我的项目的顶级目录调用以下命令:

  ./ gradlew干净
./gradlew测试
 

我也试着运行摇篮包装外的测试,并得到了相同的结果。

 摇篮干净
摇篮测试
 

解决方案

这被证实为一个问题,Robolectric 2.4(问题#1385)。这个问题已经被今日收由埃里希·道格拉斯,具有以下意见:

  

我们正在为3.0 appcompat支持。在此之前,有没有   什么可以做。

<一个href="https://github.com/robolectric/robolectric/issues/1385">https://github.com/robolectric/robolectric/issues/1385

所以,直到3.0发布时,我将使用以下解决方法:

  ./ gradlew干净
./gradlew组装
./gradlew测试
 

一旦组装已经运行,它创造了所有Robolectric显然需要的项目。然后你就可以成功的运行测试。该解决方案是,不只是为更好./ gradlew建 ./ gradlew测试的两倍,因为它可以既实现当地还有一个CI工具如特拉维斯或詹金斯。语法运行在詹金斯多个摇篮任务的各个任务之间留一个空格:

 组装测试
 

I am working on upgrading my codebase to Gradle 2.2 and Android Studio 1.0. I am currently trying to get Robolectric 2.4 working, but I am experiencing a strange issue when I try and run Unit Tests. The issue occurs only after a gradle clean; running the test suite multiple times will produce passing tests (as expected). When I run the tests after a clean, I get the following error:

java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException

I have traced the error back to this call:

Activity activity = Robolectric.setupActivity(MainActivity.class);

I experience this error whether I use the Robolectric gradle plugin (org.robolectric:robolectric-gradle-plugin:0.14.0) or the JC and K Android unit test plugin (com.github.jcandksolutions.gradle:android-unit-test:2.1.1).

I found this issue referenced on the Robolectric Github, but it doesn't look like it has been addressed yet: https://github.com/robolectric/robolectric/issues/1385

This issue is also referenced in the android studio unit test plugin, under 'Trouble Shooting': https://github.com/evant/android-studio-unit-test-plugin

My current sample code is here: https://github.com/KioKrofovitch/robolectric-upgrade-test

I have been able to run the api-android-16 project on the Robolectric samples without seeing this issue, though api-android-19 and api-android-21 projects fail for other reasons. I cannot see what they are doing differently such that they do not get this failure. https://github.com/robolectric/robolectric-samples

Has anyone found a workaround to this? Running the tests twice is not a good workaround for CI tools like Jenkins or Travis.

EDIT: Embedding code samples

My top level build.gradle where I add the JC and K Unit test library:

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()
    }
}

My project level build.gradle, where I add in 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'
}

My very basic Activity, everything has been left as the template creates it:

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);
    }
}

My dummy test, where I can't get to my assert due to the setupActivity() method error:

@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);
    }

}

EDIT #2: To run the tests I invoke the following commands from the top level directory of my project:

./gradlew clean
./gradlew test

I have also tried running the tests outside the gradle wrapper, and get the same results

gradle clean
gradle test

解决方案

This was confirmed as an issue with Robolectric 2.4 (Issue #1385). The issue has been closed by Erich Douglass today, with the following comment:

We are working on appcompat support for 3.0. Until then, there's not much you can do.

https://github.com/robolectric/robolectric/issues/1385

So until 3.0 is released, I will be using the following workaround:

./gradlew clean
./gradlew assemble
./gradlew test

Once the assemble has run, it has created all the items that Robolectric apparently needs. Then you can run your tests successfully. This solution is better than just running ./gradlew build or ./gradlew test twice, because it can be implemented both locally as well as on a CI tool like Travis or Jenkins. The syntax for running multiple Gradle tasks in Jenkins is leaving a single space between each task:

assemble test

这篇关于ClassCastException异常:出现NoClassDefFoundError不能转换为RuntimeException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆