【问题标题】:Android Studio cannot resolve org.junitAndroid Studio 无法解析 org.junit
【发布时间】:2022-04-16 21:49:34
【问题描述】:

我在 Android Studio 1.2.2 中使用 JUnit 测试。测试运行没有问题。唯一让我困惑的是 Android Studio 无法解析实际的 org.junit 包。显然它确实解决了它,否则测试将无法运行。但是导入和注释在 Android Studio 中被标记为红色,如下所示。这是 Android Studio 中的错误吗?我尝试重新构建应用并重新启动 Android Studio,但这并不能解决问题。

import org.junit.Test;

import static org.junit.Assert.*; // cannot resolve symbol 'junit'

public class CanadaTest
{
    @Test
    public void testCountryName() throws Exception
    {
        int x = 0;
        x++;
    }
}

【问题讨论】:

  • 发布您的build.gradle 文件。

标签: android android-studio junit


【解决方案1】:

试试File -> Invalidate Caches / Restart

【讨论】:

  • 在构建变体中,您的测试工件是“单元测试”?
  • 是的。如前所述,一切都已正确配置并且测试运行。如果配置不正确,测试将无法运行,AS 会抱怨。
  • 尝试“使缓存失效并重新启动”
  • 有史以来最短的答案。
【解决方案2】:

把它放在你的 gradle 文件中:

 testCompile 'junit:junit:4.12'

【讨论】:

  • 你应该把它放在testCompile 中进行单元测试。使用androidTestCompile 仪器测试。
  • 是的,只是想尝试一下
  • @JaredBurrows 这应该是一个答案。解决了我的错误。谢谢。
  • testCompile 现在已过时,对于较新的项目,这可能会有所帮助:“配置 'testCompile' 已过时,已被 'testImplementation' 和 'testApi' 取代。”
  • 使用testImplementation 'junit:junit:4.12',因为testCompile 已过时
【解决方案3】:

如果以上方法都不起作用,则可能是您的测试类位于错误的目录中。

我遇到了这个问题,因为我的文本类位于 /main/java 目录中,而不是它们应该在的 /test 目录中...

简而言之:

确保您的测试类位于/test 目录中!

【讨论】:

  • 谢谢你,真的救了我一天,因为我错误地把它放在了单元测试目录中。
【解决方案4】:

我遇到了这个问题,结果证明这只是因为我将构建变体设置为“发布”。一旦我将它改回“调试”,Android Studio 就会进行构建,当它完成时,junit 注释上的红色错误就消失了。 pic of build variant setting in android studio

【讨论】:

  • 我也这样做了,这有点道理。任何有更多构建经验的人都愿意解释原因。
【解决方案5】:

我通过结合一些答案和一些更改来解决它:

1.确保设置准备targetSdkVersioncompileSdkVersionbuildToolsVersion

2.我用了最后一个Android Gradle Plugin VersionGradle Version

3.在应用级别向buil.gradle添加一些行(我用'\*'标记它们):

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        applicationId "com.zima.testapp"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"   //*
        apply plugin: 'maven'   //*
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    dependencies {
        testImplementation 'junit:junit:4.12'  //*
        implementation 'junit:junit:4.12'   //*
        androidTestImplementation 'com.android.support:support-annotations:28.0.0'  //*
        androidTestImplementation 'com.android.support.test:runner:1.0.2'  //*
        androidTestImplementation 'com.android.support.test:rules:1.0.2'  //*
        implementation fileTree(dir: 'libs', include: ['*.jar'])   //*
        implementation fileTree(dir: 'libs', include: 'Parse-*.jar')   //*
    }
}

4.build.gradle 在项目级别是这样的:

    buildscript {
     repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        apply plugin: 'java'  //*
         }
}
allprojects {
    repositories {
        jcenter()
        google()
        maven {
            url "https://jitpack.io"
        }
    }
}

【讨论】:

    【解决方案6】:

    问题
    org.junit.* 的类和注释未在检测测试 (androidTest) 中找到。这些类是在 junit 测试 (test) 中找到的。

    解决方案
    Build VariantRelease 切换到 Debug

    【讨论】:

    • 谢谢兄弟,我欠你一个!
    【解决方案7】:

    可能在你的 gradle 文件中你有这样的东西:

    configurations {
        debugImplementation.exclude group: "junit", module: "junit"
    }
    

    删除后一切正常。

    【讨论】:

      【解决方案8】:

      如果您重新安装了 android studio,请记住默认情况下它不使用您的系统 JDK(项目结构 -> SDK 位置 -> jdk 位置)。

      【讨论】:

        【解决方案9】:

        junit 更改为最新版本

        testImplementation 'junit:junit:'

        testImplementation 'junit:junit:4.13.2'

        【讨论】:

          猜你喜欢
          • 2013-02-12
          • 1970-01-01
          • 1970-01-01
          • 2018-01-28
          • 2015-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多