【问题标题】:Android Studio Junit tests package org.junit does not existAndroid Studio Junit 测试包 org.junit 不存在
【发布时间】:2016-04-01 15:42:52
【问题描述】:

我正在尝试在 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,但仍然无法正常工作。

【问题讨论】:

  • 你是在写单元测试吗?(/src/test)还是ui测试(src/androidTest)?
  • 是的,它们是 Instrumented 单元测试,所以为了运行它们,它们必须在 src/androidTest 中,据此:developer.android.com/intl/es/training/testing/start/…
  • 看这里:github.com/googlesamples/android-testing/tree/master/ui/…。使用 Junit 单元进行单元测试。
  • 好的,我在那个 repo 上找到了一些东西,但在那个项目上没有。无论哪种方式,“看这里”和 GitHub 存储库的 URL 都不是一个很好的答案......无论如何,谢谢。
  • “无论如何,“看这里”和 GitHub 存储库的 URL 都不是一个很好的答案”。真的吗?你是需要帮助的人。我没有提供答案,这只是一个评论。由于您是 Android 新手,因此应该使用 Google 示例。

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


【解决方案1】:

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

我没有这个依赖,所以我将它们添加到 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'

【讨论】:

猜你喜欢
  • 2020-10-23
  • 2015-02-06
  • 1970-01-01
  • 2011-08-16
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
相关资源
最近更新 更多