gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:50 29 4
gpt4 key购买 nike

我正在尝试在 Android Studio 中实现一个测试类以对 DBAdapter 进行一些测试。因为我需要在我的手机上运行测试才能使用数据库,所以我创建了一个 Instrumentation 单元测试(因为我试图只用一个单元测试来做,但我需要使用数据库等等,并且那些只在本地运行)。

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

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

最佳答案

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

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

关于Android Studio Junit 测试包 org.junit 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36360450/

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