2016-05-07 105 views
2

我註解班裏喜歡這裏AndroidJUnit4測試沒有發現

@RunWith(AndroidJUnit4.class) 
public class WorkdayProviderTest 

Futhermore,也註釋這樣

@Test 
public void insert_dataInsertsCorrectly() 

我的測試方法。最後,配置了我的build.gradle defaultConfig和依賴這樣

defaultConfig { 
    applicationId 'com.jueggs.workinghours' 
    minSdkVersion 16 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
} 

androidTestCompile "junit:junit:4.12" 
androidTestCompile "com.android.support:support-annotations:23.3.0" 
androidTestCompile "com.android.support.test:runner:0.4.1" 
androidTestCompile "com.android.support.test:rules:0.4.1" 
androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.1" 
androidTestCompile "com.android.support.test.espresso:espresso-contrib:2.2.1" 
androidTestCompile "org.hamcrest:hamcrest-library:1.3" 

並配置了這樣的測試運行

enter image description here

和it's告訴我

No tests were found 

Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException' 

如何解決這個問題?

回答

1

您的Gradle依賴關係中可能沒有東西。您是否正確申報com.android.support.test:runner(可選com.android.support.test:rules)?

請看看我的build.gradle文件:

apply plugin: 'com.android.application' 
apply plugin: 'android-apt' 

buildscript { 
    repositories { 
     jcenter() 
    } 

    dependencies { 
     classpath 'com.android.tools.build:gradle:1.5.0' 
     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6' 
    } 
} 

apt { 
    arguments { 
     androidManifestFile variant.outputs[0].processResources.manifestFile 
     resourcePackageName "com.piotr.testexample" 
    } 
} 

repositories { 
    mavenCentral() 
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 
} 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    useLibrary 'org.apache.http.legacy' 

    //For building with Travis CI 
    lintOptions { 
     abortOnError false 
    } 

    packagingOptions { 
     exclude 'META-INF/services/javax.annotation.processing.Processor' 
    } 

    defaultConfig { 
     applicationId "com.piotr.testexample" 
     minSdkVersion 16 
     targetSdkVersion 22 
     versionCode 1 
     versionName "1.0" 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 

    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 
dependencies { 
    ext.JUNIT_VERSION = '4.12' 
    ext.AA_VERSION = '4.0.0' 
    ext.SUPPORT_VERSION = '23.3.0' 
    ext.ESPRESSO_VERSION = '2.2.2' 

    apt "org.androidannotations:androidannotations:$AA_VERSION" 
    compile "org.androidannotations:androidannotations-api:$AA_VERSION" 

    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile "junit:junit:$JUNIT_VERSION" 
    testCompile("org.robolectric:robolectric:3.0") { 
     exclude module: 'classworlds' 
     exclude module: 'commons-logging' 
     exclude module: 'httpclient' 
     exclude module: 'maven-artifact' 
     exclude module: 'maven-artifact-manager' 
     exclude module: 'maven-error-diagnostics' 
     exclude module: 'maven-model' 
     exclude module: 'maven-project' 
     exclude module: 'maven-settings' 
     exclude module: 'plexus-container-default' 
     exclude module: 'plexus-interpolation' 
     exclude module: 'plexus-utils' 
     exclude module: 'wagon-file' 
     exclude module: 'wagon-http-lightweight' 
     exclude module: 'wagon-provider-api' 
    } 

    compile "com.android.support:appcompat-v7:$SUPPORT_VERSION" 
    compile "com.android.support:design:$SUPPORT_VERSION" 
    compile "com.android.support:cardview-v7:$SUPPORT_VERSION" 
    compile "com.android.support:recyclerview-v7:$SUPPORT_VERSION" 

    compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT' 

    compile 'com.squareup.retrofit:retrofit:1.9.0' 
    compile 'com.google.code.gson:gson:2.4' 
    compile 'com.squareup.picasso:picasso:2.5.2' 

    androidTestCompile "com.android.support:support-annotations:$SUPPORT_VERSION" 
    androidTestCompile "com.android.support.test.espresso:espresso-core:$ESPRESSO_VERSION" 
    androidTestCompile 'com.android.support.test:runner:0.5' 
    androidTestCompile "com.android.support.test.espresso:espresso-intents:$ESPRESSO_VERSION" 
    /** 
    * AccessibilityChecks 
    * CountingIdlingResource 
    * DrawerActions 
    * DrawerMatchers 
    * PickerActions (Time and Date picker) 
    * RecyclerViewActions 
    */ 
    androidTestCompile("com.android.support.test.espresso:espresso-contrib:$ESPRESSO_VERSION") { 
     exclude group: 'com.android.support', module: 'appcompat' 
     exclude group: 'com.android.support', module: 'support-v4' 
     exclude group: 'com.android.support', module: 'support-v7' 
     exclude group: 'com.android.support', module: 'design' 
     exclude module: 'support-annotations' 
     exclude module: 'recyclerview-v7' 
    } 

    compile "junit:junit:${JUNIT_VERSION}" 
} 

編輯:問題是與註釋,請執行以下解決方法之一:

  • 刪除@RunWith(AndroidJUnit4.class)
  • 變化@RunWith(AndroidJUnit4.class)@RunWith(JUnit4.class)

測試包中似乎沒有更多AndroidJUnit4.class

+0

是我有這個依賴關係,查看更新 – Lemao1981

+1

刪除'@RunWith(AndroidJUnit4.class)'並運行test。你也可以用'@RunWith(JUnit4.class)'改變'@RunWith(AndroidJUnit4.class)',它應該運行。 – piotrek1543

+0

沒有解決問題,在兩種情況下都找不到任何測試。在0.4.1版本中,類AndroidJUnit4存在 – Lemao1981

-1

嘗試命名你的類和方法在這fashon:

  • Test_MyClass
  • test_myMethod

我希望這有助於。

+0

不,沒有幫助 – Lemao1981

+0

對不起。我用Espresso Instrumentation測試以這種方式解決了這個問題。無論如何,在單元測試中,我使用Robolectric。如果你會覺得卡住了,試試吧! –

+0

在android studio中,在Build Variants中,是否已將儀表測試切換到單元測試? –

0

好的,問題與我的測試配置沒有關係,但是另一個類缺失,我很快就刪除了。該應用程序本身甚至沒有開始。可惜類名沒有提到無處可以

相關問題