18

코드베이스를 Gradle 2.2 및 Android Studio 1.0으로 업그레이드하려고합니다. 현재 Robolectric 2.4 작동을 얻으려고하고 있지만 Unit Tests를 실행하려고 할 때 이상한 문제가 발생했습니다. 이 문제는 gradle clean 이후에만 발생합니다. 테스트 스위트를 여러 번 실행하면 (예상대로) 통과 테스트가 생성됩니다. 나는 Robolectric Gradle을 플러그인을 사용할지 여부를 내가이 오류가 발생ClassCastException : NoClassDefFoundError를 RuntimeException에 캐스팅 할 수 없습니다.

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

:이 호출에 다시 오류를 추적 한

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

: 나는 깨끗한 후 테스트를 실행하면, 나는 다음과 같은 오류가 발생합니다 (org.robolectric:robolectric-gradle-plugin:0.14.0) 또는 JC 및 K Android 기기 테스트 플러그인 (com.github.jcandksolutions.gradle:android-unit-test:2.1.1).

은 내가 Robolectric Github에서에 참조 된이 문제를 찾았지만, 아직 해결 된 것처럼은 보이지 않는 : https://github.com/robolectric/robolectric/issues/1385

이 문제는 또한 안드로이드 스튜디오 유닛 테스트 플러그인에서 참조를, '트러블 슈팅에서 ' https://github.com/evant/android-studio-unit-test-plugin

나의 현재 샘플 코드가 여기에 있습니다 : 나는 생기고없이 Robolectric 샘플의 API를-안드로이드-16 프로젝트를 실행 할 수 있었다 https://github.com/KioKrofovitch/robolectric-upgrade-test

이 문제는 api-android-19 및 api-android-21 프로젝트가 다른 이유로 실패하는 경우에도 발생합니다. 나는 그들이 그들이이 실패를 얻지 않도록 다르게하고있는 것을 볼 수 없다. https://github.com/robolectric/robolectric-samples

누구나 해결 방법을 찾았습니까? 테스트를 두 번 실행하는 것은 Jenkins 또는 Travis와 같은 CI 도구에 대한 좋은 해결 방법이 아닙니다.

편집 : 임베드 코드 샘플 내가 JC와 K 단위 테스트 라이브러리를 추가

내 최고 수준의 build.gradle :

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

내가 robolectric에 추가 내 프로젝트 수준의 build.gradle :

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' 
} 

템플릿을 생성으로 내 아주 기본적인 활동, 모든 것이 남아있다 : ​​

,691 내가 인해 setupActivity() 메소드 오류로 내 주장 얻을 수 없다
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); 
    } 
} 

내 더미 테스트 :

@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 
+1

실제로이 질문에 관련 코드를 붙여 넣을 수 있습니까? 내가보고 있어야하는 github 프로젝트에서 어떤 파일이 있는지 전혀 알지 못합니다. –

+0

예, 고마워요! 가장 관련있는 코드 스 니펫 –

+0

을 추가했습니다. 확실히 gradle 문제, 그리고 나는 gradle에 관해서는 거의 알지도 모릅니다. 그래서 이것은 당신을 도울 수있는 능력을 초월합니다. 내가 한 일은 질문에서 안드로이드 태그 대신에 gradle 태그를 사용하는 것입니다. 일부 gradle 전문가가 찾아와 귀하의 질문을 찾을 수 있기를 바랍니다. 미안하다. 나는 더 이상 도움이 될 수 없다. –

답변

13

이것은 Robolectric 2.4 (문제 # 1385)의 문제로 확인되었습니다.이 문제는 다음과 같은 코멘트와 함께, 오늘 에리히 더글라스에 의해 폐쇄 된 다음 번

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

: 3.0이 해제 될 때까지

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

그래서, 나는 다음과 같은 해결 방법을 사용하는 것 assemble이 실행되면 Robolectric이 분명히 필요로하는 모든 항목을 만들었습니다. 그런 다음 테스트를 성공적으로 실행할 수 있습니다. 이 솔루션은 ./gradlew build 또는 ./gradlew test을 두 번 실행하는 것보다 낫습니다. Travis 또는 Jenkins와 같은 CI 도구에서 로컬로 구현 될 수 있기 때문입니다. Jenkins에서 여러 개의 Gradle 작업을 실행하는 구문은 각 작업 사이에 하나의 공백을 남겨두고 있습니다.

assemble test 
+0

다른 해결 방법이 있습니까? 이것은 나를 위해 작동하지 않습니다. –

0

내 문제가 해결되었습니다. 내 robolectirc를 2.4로 업데이트하고 SDK를 min으로 19, android support를 com.android.support:support-v13:19.1.0으로 업데이트했습니다. 그리고 같은 것을 위해 iml 파일을 변경했습니다. 여러분의지도와 응답

벤 카트 라만