![](/img/trans.png)
[英]Test case success with Unit test but fails with Instrumented Test with NoSuchMethodError
[英]Android Instrumented unit test on project library
我繼承了一個由主應用程序和一個Library組成的Android Project。 我的目標是測試整個項目,所以我從庫開始:首先我使用JUnit測試一些類/方法,編寫我的庫項目的測試undeer app / src / test / java(單元測試),並生成一個Android Studio的代碼覆蓋率報告。 當我找到一個使用Parcel的類(來自Android Framework)時,問題就來了,所以這一次,我不得不在Library項目中也設置了一個已測試的測試(app / src / androidTest / java)。
當我嘗試運行檢測到的測試並插入Android設備時,收到以下消息:
Started running tests
Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
Empty test suite.
我不明白為什么在測試類中有幾個@Test方法時會“清空測試套件”。 我是否已設置Gradle如何在測試期間鏈接主應用程序和庫,或者可能必須將所有測試都移至主應用程序中?
這是我為圖書館模塊准備的:
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
// Added for instrumental tests
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
testCoverageEnabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
implementation "commons-io:commons-io:$rootProject.commonsioVersion"
// Dependencies for RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Dependencies for Rxjava2 and RxAndroid
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.8'
// Dependencies for RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Dependencies for Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.squareup.okhttp:okhttp:2.7.0'
// Dependencies for Room
// Room (use 1.1.0-alpha1 for latest alpha)
implementation 'android.arch.persistence.room:runtime:1.1.1'
implementation 'android.arch.persistence.room:rxjava2:1.1.1'
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
implementation "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
implementation "org.apache.httpcomponents:httpcore:$rootProject.httpcoreVersion"
testImplementation 'junit:junit:4.12'
// // [Android Instrumented test]
androidTestImplementation 'junit:junit:4.12'
// androidTestImplementation 'androidx.test:runner:1.1.0'
// androidTestImplementation 'androidx.test:rules:1.1.0'
// // Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// // Optional -- UI testing with Espresso
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
// // Optional -- UI testing with UI Automator
// androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
api files('libs/lifesense_ble_v3.3.7.jar')
api files('libs/MedPixel.jar')
}
和應用程序的:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "it.*******.********"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 2
versionName "1.0.2"
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
debuggable true
}
}
lintOptions {
checkReleaseBuilds false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation project(':..:Libraries:app')
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
implementation "commons-io:commons-io:$rootProject.commonsioVersion"
// Dependencies for ButterKnife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
// Dependencies for Dagger
implementation 'com.google.dagger:dagger:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
// Dependencies for RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Dependencies for Rxjava2 and RxAndroid
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.8'
// Dependencies for Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.squareup.okhttp:okhttp:2.7.0'
// Dependencies for Room
implementation 'android.arch.persistence.room:runtime:1.1.1'
// Dependencies for Roboelecttic
testImplementation 'org.robolectric:robolectric:3.6.1'
testImplementation 'junit:junit:4.12'
// Dependencies for Mockito
testImplementation 'org.mockito:mockito-core:2.7.22'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.squareup.retrofit2:retrofit-mock:2.0.0'
androidTestImplementation 'com.squareup.okhttp:mockwebserver:2.7.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
}
configurations.all {
resolutionStrategy {
force "android.arch.core:runtime:1.1.1"
force "com.android.support:support-core-utils:28.0.0"
/*force "com.android.support:support-compat:28.0.0"*/
}
}
感謝您的關注!
我遇到了同樣的問題,不是Parcel
而是其他Android組件(如Context
。
對於這個問題
空測試套件
您的檢測測試類必須使用 @RunWith(AndroidJUnit4.class)
注釋 。
最初,我確實在app
編寫了Instrumented測試。 但是在app
編寫庫的InstrumentationTests
的問題是,您將不會獲得library
的覆蓋率報告。
因此,我將InstrumentedTests
移回了library/androidTest
文件夾。 經過大量研究,我找到了一種解決方案,可獲取library
完整覆蓋率報告
在庫的build.gradle
文件中執行以下操作以獲得覆蓋率
添加apply plugin: 'jacoco'
啟用如下測試范圍
android { buildTypes { debug { testCoverageEnabled true } } ... }
添加以下依賴項
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:rules:1.0.2'
我相信,對於您項目中的任何模塊,如果您的模塊/庫中都存在androidTests
,那么將存在一個默認任務,稱為combinedTestReportDebug
。
您可以在Android Studio右上角垂直欄中的Gradle
菜單中找到它。
Gradle menu > :your-library > Tasks > reporting > combinedTestReportDebug
要么
從在您的Terminal
> ./gradlew clean combinedTestReportDebug -p :your-library
運行此命令
測試完成后,您可以在以下位置找到覆蓋率或失敗的測試
對於失敗的測試: your-library/build/reports/androidTests/connected/index.html
對於覆蓋率報告: your-library/build/reports/coverage/debug/index.html
我希望這有幫助。
抱歉,我忘了提到需要執行的以下步驟,因為如果未combinedTestReportDebug
則看不到TestTestDebug。 感謝@nicoabie。
在項目的build.gradle
文件中,執行以下操作,以在您的庫gradle任務中使combinedTestReportDebug
任務可見。
buildscript {
repositories {
jcenter()
}
dependencies {
...
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.13.0'
...
}
}
...
apply plugin: 'com.vanniktech.android.junit.jacoco'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.