gpt4 book ai didi

带有 Robolectric : my class test is not found 的 Android Studio

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:58 26 4
gpt4 key购买 nike

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

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

Class not found: "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);
}
}

我的文件夹架构是:

  • 我的应用

    • 来源

      • 主要

        • java
          • com.example.myapp ...
      • 测试

        • java
          • com.example.myapp ...

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

最佳答案

好吧,我找到了一个解决方案:

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

关于带有 Robolectric : my class test is not found 的 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com