Android Studio Junit测试包org.junit不存在

时间:2016-04-01 15:42:53

标签: android unit-testing android-studio junit android-instrumentation

我尝试在Android Studio中实现测试类,以便对DBAdapter进行一些测试。由于我需要在我的手机上运行测试才能使用数据库,因此我创建了一个仪器单元测试(因为我试图只使用单元测试,但我需要使用数据库,所以,那些只在本地运行)。

问题是当我尝试运行测试类时,使用我的手机作为运行设备,编译器会抛出以下错误:

error: package org.junit does not exist

我一直在寻找解决方案,但我找不到。

这是我的测试类(只是骨架):

import org.junit.Test;
import android.support.test.runner.AndroidJUnit4;

import static org.junit.Assert.*;


public class DbAdapterTest {

    @Test
    public void testCreateSeries() throws Exception {

    }
}

这个是build.gradle脚本:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.w2w.whattowatch"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"
}

我还有另一个问题。如你所见,我也导入了这个:

import android.support.test.runner.AndroidJUnit4;

但是,即使在跑步之前,也会说"跑步者"那"无法解析符号"。我已经在build.gradle上添加了TestInstrumentationRunner,但仍无效。

1 个答案:

答案 0 :(得分:4)

好的,我解决了,这是对我有用的解决方案。

我没有这种依赖,所以我将它们添加到build.gradle脚本中:

androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'