显然我需要正确的import语句来解决这个问题。根据docs for AndroidJUnit4
,这应该是
import android.support.test.runner.AndroidJUnit4;
当我这样做时,Android Studio以红色突出显示runner
并抱怨“无法解析符号'跑步者'”。
背景
我按照Android开发者网站上的setting up tests using UI Automator教程了解了这一点。我遇到的第一个问题是com.android.support:support-v4:22.2.0
和com.android.support.test:runner:0.2
依赖于不同版本的com.android.support:support-annotations
。我按照this Android bug report的建议,并在我的项目的allprojects
中将以下内容添加到build.gradle
:
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}
这解决了即时错误,但我怀疑它导致了我当前的问题。有没有人有任何关于如何解决这个问题的建议?
来自`./gradlew:app:dependencies的相关部分
androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| \--- org.easytesting:fest-assert-core:2.0M10
| \--- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0
compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| \--- com.android.support:support-v4:22.2.0
| \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
\--- com.jakewharton:butterknife:5.1.2
将测试类移动到src / androidTest / java /。然后依赖将解决。
添加
compile com.android.support.test:runner:0.5'
为我解决了这个问题。
正如答案列表所示,这可能是由一些事情引起的。列表还有一个:
我跑了一个过度热心的LINT,删除了所有未使用的进口。这将产生相同的错误,很容易错过这是问题所在。
Android-studio将突出显示测试代码中缺少的引用 - 并且将出现ALT-ENTER弹出窗口(这是容易错过的位)。
接下来,我需要从LINT中删除测试 - 或者至少禁用此警告。
编辑:@ Code-Apprentice,缺少的行是:
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
因此,文件中的第一个错误是在我的测试类开头使用@RunWith(AndroidJUnit4.class)
。
经典的Invalidate Caches / Restart帮助了我! :)
我犯了错误,把测试类放在src / test上。将它们移动到src / androidTest / java /后,依赖关系得到了解决。
好的,这是你的错误,我的!
如果我们要为Local Unit Testing编写一些代码,我们不应该使用@RunWith(AndroidJUnit4.class)
因为我们不使用AndroidJUnit4但我们需要Junit4。所以我们应该写@RunWith(JUnit4.class)
。当然,您的java测试文件位于app/src/test/java/your.package.name
目录下。
否则if(!!)我们想写一些Android Instrumented Unit Test我们应该将我们的测试java文件放在app/src/androidTest/java/your.package.name
目录中并使用像@RunWith(AndroidJUnit4.class)
这样的注释
更新
Android Test Library现在是AndroidX的一部分。请务必使用official documentation中找到的正确Gradle依赖项。
原始答案
我发现here有比我使用的更新版本的测试支持库:
dependencies {
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
注意:请务必使用这些库的最新版本。这个问题是从Android测试支持库是新的,这里的版本号已经过时了。
我通过在app的build.gradle文件中进行一些小改动来解决问题。在dependencies { ... }
部分,请确保包含以下行:
debugImplementation 'com.android.support.test:runner:1.0.1'
或者当时最新的版本(...Compile
已被弃用并已被...Implementation
取代)。注意使用debugImplementation
。 Android Studio建议使用androidTestImplementation
自动包含它,但这不起作用。
通过查看app模块的Dependencies下的Project Structure,我发现了如何将它从测试更改为调试,您可以在其中更改每个依赖项的范围,请参阅下文。
就我而言,这有助于发布变体:
android {
...
testBuildType "release"
}
如果有人还有这个问题:
无法解析符号'AndroidJUnit4'
并使用API 27,在app模块中的build.gradle
中添加以下行:
testImplementation 'junit:junit:4.12'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
将此代码放在您的依赖项中
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
如果您正在使用具有多个构建类型的项目,则必须在模块的build.gradle文件中使用testBuildType标记提及构建变量窗口中的所选构建类型。
例如:如果您正在使用构建类型调试,那么您应该添加android{testBuildType "debug" }
,如果使用stage,则在android标记中添加android{testBuildType "stage"}
语句。