android.support.test不存在,Android Studio Junit测试包org.junit不存在

我正在尝试在Android Studio中实现一个测试类,以便对DBAdapter进行一些测试.由于我需要在手机上运行测试才能使用数据库,因此我创建了一个Instrumentation Unit Test(因为我试图仅通过Unit test来进行测试,但是我需要使用数据库,如此,这些仅在本地运行).

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio可以用于进行Android应用的自动化测试。在Android Studio中,你可以使用Android Testing Support Library和Espresso库来编写自动化测试代码。通过Espresso库,你可以模拟用户与应用程序进行交互,并验证应用程序的行为。例如,你可以使用Espresso来测试应用程序的UI元素是否显示正确,点击按钮后是否触发正确的操作等。 为了编写自动化测试代码,你需要创建一个Android测试项目,并在该项目中编写测试用例。你可以使用JUnit测试框架来编写测试用例,使用Espresso提供的API来模拟用户交互和验证应用程序的行为。 以下是一个简单的示例代码,演示如何使用Espresso来对应用程序进行自动化测试: ```kotlin @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @get:Rule val activityRule = ActivityScenarioRule(MainActivity::class.java) @Test fun testButtonClick() { // 模拟点击按钮 onView(withId(R.id.button)).perform(click()) // 验证文本视图的文本是否与预期相符 onView(withId(R.id.textView)).check(matches(withText("Button Clicked"))) } } ``` 上面的代码中,`ExampleInstrumentedTest`是一个测试类,使用`@RunWith(AndroidJUnit4::class)`注解来指定使用JUnit4运行测试。`@get:Rule`用于创建一个ActivityScenarioRule,用于启动被测试的应用程序的MainActivity。 `testButtonClick`方法是一个测试用例,使用`onView`来选择要操作的UI元素(这里是一个按钮),然后使用`perform`来模拟用户的操作(这里是点击按钮)。最后,使用`check`和`matches`来验证应用程序的行为(这里是验证文本视图的文本是否与预期相符)。 请注意,这只是一个简单的示例,你可以根据具体的测试需求编写更复杂的测试用例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值