使用Robolectric的Android Studio:找不到我的班级测试

问题描述 投票:5回答:1

这是我的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'
} 

这是我的根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' }
}
}

我只是编写一个简单的类来测试以验证我的基础结构测试,但是我有这个错误:

找不到类:“com.example.myapp.activity.BaseActivityTest”

我的测试类是:

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

我的文件夹架构是:

  • MYAPP SRC 主要 java的 com.example.myapp ... 测试 java的 com.example.myapp ...

我不明白为什么会出现这个错误。

android unit-testing android-studio gradle robolectric
1个回答
0
投票

好的,我找到了解决方案:

这是我的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.+'
}

我修改了我的VM参数(配置版),就像这篇文章:http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

© www.soinside.com 2019 - 2024. All rights reserved.