Android Studio 集成测试无法解析符号 AndroidJUnit4 [英] Android Studio Integration test cannot resolve symbol AndroidJUnit4

查看:57
本文介绍了Android Studio 集成测试无法解析符号 AndroidJUnit4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到过类似问题的部分答案,但从未真正解决过问题.我已经把它归结为一个最小的应用程序来演示这个问题.

I've seen partial answers to similiar issues but never one that actually solved the problem. I've boiled this down to a minimal app to demonstrate the issue.

我使用的是最新版本的 Android Studio (V2.2.1)我安装了以下 SDK:

I'm using the most current version of Android Studio (V2.2.1) I have the following SDK's installed:

  • Android SDK 工具 v25.1.6
  • Android SDK 平台工具 v23.1
  • Android SDK Build-tools v23.0.3
  • Android 支持存储库 v32

  • Android SDK Tools v25.1.6
  • Android SDK Platform-tools v23.1
  • Android SDK Build-tools v23.0.3
  • Android Support repository v32

  1. 使用向导创建新的 Android 应用
  2. 更新了 build.gradle(应用):https://developer.android.com/training/testing/start/index.html#config-instrumented-tests

  • 在defaultConfig下添加:

  • Added under defaultConfig:

  • testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

在依赖项下添加

Added under dependencies

  • androidTestCompile 'com.android.support:support-annotations:23.0.1' <-- 更改为 23.4.0 以匹配新生成的应用
  • androidTestCompile 'com.android.support.test:runner:0.4.1'
  • androidTestCompile 'com.android.support.test:rules:0.4.1'

项目是否同步以同步 Gradle

Did project sync to sync Gradle

在test"包下添加新的测试文件 - ExampleIntegrationTest.java(就在 ExampleUnitTest.java 旁边)

Under the "test" package add new test file - ExampleIntegrationTest.java (right next to ExampleUnitTest.java)

在新文件中,创建集成测试:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

In the new file, created integration test per: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import org.junit.runner.RunWith;


@RunWith(AndroidJUnit4.class)
@SmallTest
public interface ExampleIntegrationTest {
}


这就是问题所在 - 无法解析符号 AndroidJUnit4上线:import android.support.test.runner.AndroidJUnit4;


This is where the problem comes up - cannot resolve symbol AndroidJUnit4 On the line: import android.support.test.runner.AndroidJUnit4;

如果我输入 import 语句,我可以使用弹出式建议:
- 导入安卓.- 它显示支持作为选项"
- 导入 android.support.- 它显示测试"作为一个选项
- 导入 android.support.test.- 现在它只显示规则"作为选项

If I type the import statement I can use the popup suggestions:
- import android. - It shows "support as option"
- import android.support. - It shows "test" as an option
- import android.support.test. - Now it only shows "rule" as a option

创建应用程序后,我确保它使用调试"变体

After I create the app I make sure it's using the "debug" variant

这是非常干净和干燥的.据我所知,这应该可行,但由于某种原因无法正确导入支持库.

This is pretty cut and dry. As far as I can tell this should work,, but the support library for some reason isn't getting imported correctly.

推荐答案

通过将测试添加到

  • /app/src/androidTest 目录而不是
  • /app/src/test 仅用于单元测试的目录

或者,当您创建测试类时,通过在项目树的Android Instrumentation Tests"选项卡中选择新类来添加测试

Or, when you create the test class, add the test by selecting new class when in "Android Instrumentation Tests" tab of the Project tree

这篇关于Android Studio 集成测试无法解析符号 AndroidJUnit4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆