我遇到了一个问题,这个问题似乎在这个平台上很受欢迎,但似乎没有一个答案对我有效。我正面临着一个类似的问题:
Error:(23, 17) Failed to resolve: junit:junit:4.12
但是,43个答案中没有一个适用于我的问题。(我希望这不会被标记为重复)
我曾尝试手动将org.junit包导入到测试类中,但无法解析。我认为原因是Gradle不能加载JUnit库,但我不知道如何改变这一点。
即使创建了一个新的android studio项目,我在设置JUnit时也遇到了问题。即使是示例测试也不能运行:
package de.ckc.azubis.fleetparkmanagement;
import static junit.framework.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine
(host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}当我尝试导入测试注释时,JUnit依赖项被添加到gradle文件中,但没有导入到测试类中。
下面是我的模块级build.gradle:
应用插件:'com.android.application‘
android {
compileSdkVersion 27
defaultConfig {
applicationId "de.ckc.azubis.fleetparkmanagement"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:cardview-v7:27.0.2'
}和我的项目级build.gradle
//顶级构建文件,可以添加所有子项目/模块通用的配置选项。
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}我已经尝试了我能想到的所有方法,包括重新配置依赖关系,手动添加我能想到的每个存储库(mavenCentral,jcenter...),检查我的代理设置等等。
发布于 2018-02-13 23:47:13
缓存有问题。我通过点击file -> Invalidate Caches然后重新启动来修复它
发布于 2021-09-02 07:03:59
我通过从设置-> JunitGenerator ->属性->Default模板中选择Junit4解决了此问题:


https://stackoverflow.com/questions/48768989
复制相似问题