Android Studio无法找到JUnit包

时间:2015-10-06 02:21:13

标签: android android-studio android-gradle junit4

我似乎无法获得测试文件来查找Junit4。我一直在努力,我只是感到沮丧。它说它无法在org下找到符号junit。我搜索过的所有东西(几天),甚至安卓文档都说这是怎么做的。我做错了什么?

这是我的测试类

import org.junit.Test;
import android.app.Application;



public class ApplicationTest {
  //  @Test
    public ApplicationTest() {


    }
}

这是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
   //buildToolsVersion "21.1.2"
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "stuff.morestuffs"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile files('libs/gson-2.3.1.jar')
    testCompile 'junit:junit:4.12'
}

继承我的项目build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

我正在使用Android Studio 1.4

2 个答案:

答案 0 :(得分:3)

  1. 对于单元测试(JVM测试),根据Android测试,根源目录应为test,而不是androidTest

  2. 在" Build Variants"下,我进行了" Android乐器测试"选择。当我将其更改为" Unit Tests"时,Android Studio正确识别了我的文件。

答案 1 :(得分:1)

当我向build.gradle添加以下依赖项时,我解决了这个问题:

  

androidTestCompile'junit:junit:4.12'

如果您需要测试多个单独的方法,并且想要测试您的活动或片段,则必须添加它。