简体   繁体   English

带有 Robolectric 的 Android Studio:找不到我的课堂测试

[英]Android Studio with Robolectric: my class test is not found

This is my gradle file:这是我的gradle文件:

apply plugin: 'android'
apply plugin: 'android-test'

android {
// Check on it to know witch Android API level is necessary:
// http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
compileSdkVersion 19
buildToolsVersion '19.0.1'

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
sourceSets {
    androidTest {
        setRoot('src/test')
    }
}
// Patch: http://stackoverflow.com/questions/20673888/duplicate-files-copied-android-studio-0-4-0
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/ASL2.0'
}
}

androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}

dependencies {

// Android SDK Extra librairies
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
compile fileTree(dir: 'libs', include: ['*.jar'])

// Android testing
// http://robolectric.org/
androidTestCompile 'junit:junit:4.+'
androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'com.squareup:fest-android:1.0.+'
// had to deploy to sonatype to get AAR to work
//compile 'com.novoda:actionbarsherlock:4.3.2-SNAPSHOT'
} 

This my root gradle file:这是我的根 gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.9.+'
    //classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
    //classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
    classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}

allprojects {
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}

I just write an easy class to test to validate my infrastructure test, but I have this error:我只是编写了一个简单的类来测试以验证我的基础设施测试,但是我有这个错误:

Class not found: "com.example.myapp.activity.BaseActivityTest"找不到类:“com.example.myapp.activity.BaseActivityTest”

My test class is:我的测试课是:

package com.example.myapp.activity;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

@RunWith(RobolectricTestRunner.class)
public class BaseActivityTest {
@Test
public void testTrueIsTrue() throws Exception {
    assertEquals(true, true);
}
}

My folder architecture is:我的文件夹架构是:

  • myapp我的应用程序

    • src源文件

      • main主要的

        • java爪哇
          • com.example.myapp ... com.example.myapp ...
      • test测试

        • java爪哇
          • com.example.myapp ... com.example.myapp ...

I don't understand why this error appears.我不明白为什么会出现这个错误。

Ok guys, I found a solution:好的,我找到了一个解决方案:

This is my gradle file:这是我的gradle文件:

apply plugin: 'android'
apply plugin: 'robolectric'

android {
// Check on it to know witch Android API level is necessary:
// http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
compileSdkVersion 19
buildToolsVersion '19.0.1'

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
sourceSets {
    androidTest {
        setRoot('src/test')
    }
}
}

// prevent the "superClassName is empty" error for classes not annotated as tests
tasks.withType(Test) {
 scanForTestClasses = false
 include "**/*Test.class" // whatever Ant pattern matches your test class files
}

dependencies {

// Android SDK Extra librairies
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

// Android testing
//androidTestCompile configurations.androidTestCompile.dependencies
// http://robolectric.org/
androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'junit:junit:4.+'
}

And I modified my VM argument (Configuration edition) like this post: http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments我像这篇文章一样修改了我的 VM 参数(配置版): http : //blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM