Java Android Studio 2.1:错误:包 org.junit 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37061863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:58:02  来源:igfitidea点击:

Android Studio 2.1: error: package org.junit does not exist

javaandroidunit-testingjunit

提问by Aggressor

Update: Its a bug and it's been reported, please star: https://code.google.com/p/android/issues/detail?id=209832&thanks=209832&ts=1463161330

更新:这是一个错误,已被报告,请加星标:https: //code.google.com/p/android/issues/detail?id=209832&thanks=209832&ts=1463161330

I'm setting up unit testing on Android studio.

我正在 Android Studio 上设置单元测试。

I have read the documentationand set it up exactly as specified. I have my test folder set up as src/test/java

我已阅读文档并完全按照指定进行设置。我的测试文件夹设置为src/test/java

I've made a random test class: enter image description here

我做了一个随机测试类: 在此处输入图片说明

import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class RandomTestClass
{
    @Test
    public void testTest()
    {
        assertThat(4, is(4));
    }
}

However when I go to run my test I get:

但是,当我去运行我的测试时,我得到:

error: package org.junit does not exist

错误:包 org.junit 不存在

I've set up my gradle EXACTLY as descibed in the docs:

我已经完全按照文档中的描述设置了我的 gradle:

dependencies {
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
}

The few other questions with this issue seemed to say these dependencies are missing. I have them.

与此问题有关的其他几个问题似乎表明缺少这些依赖项。我有他们。

Can you think of any reason my Local Unit Tests are not finding the junit file when I go to run the test?

当我去运行测试时,你能想到我的本地单元测试没有找到junit文件的任何原因吗?

NoteIt's able to find the junit classes when Im writing the code. It only can't find junit when I run the test.

注意在我编写代码时可以找到junit类。当我运行测试时,它只能找到junit。

采纳答案by Aggressor

It looks like Gradle is not doing it's job.

看起来 Gradle 没有完成它的工作。

Manually adding the jarsfixed the problem.

手动添加罐子解决了这个问题。

回答by coder-don

Some things you should check -

你应该检查的一些事情 -

  • Do you have unit test and debug selected under build variants?
  • Is your working directory set to $MODULE_DIR$ in Run/Debug configurations for the unit test?
  • Did you create the test by selecting the class you wish to test, going to Navigate -> Test and having Android Studio construct the test class for you?
  • 您是否在构建变体下选择了单元测试和调试?
  • 您的工作目录是否在单元测试的运行/调试配置中设置为 $MODULE_DIR$?
  • 您是否通过选择要测试的类、转到 Navigate -> Test 并让 Android Studio 为您构建测试类来创建测试?

回答by Caner Bal?m

I changed TestCompile to androidTestCompile and it's worked without problems.

我将 TestCompile 更改为 androidTestCompile 并且它没有问题。

testCompile 'junit:junit:4.12'

to

androidTestCompile 'junit:junit:4.12'

回答by Gokhan Arik

My tests are in src/test/javafolder and adding test.setRoot('test')to sourceSetsworked for me.

我的测试在src/test/java文件夹中并添加test.setRoot('test')sourceSets对我有用。

sourceSets {
    test.setRoot('test')
}

回答by Or Harel

Try to change the Build Variant to debug.

尝试将 Build Variant 更改为debug.

View -> Tool Windows -> Build Variants

View -> Tool Windows -> Build Variants

UPD: This actually may be the cause, when all gradledependencies are met, but org.junit.Teststill not found. At least it was a cause for me. It was not working in 'release' build type, but was working in 'debug'.

UPD:这实际上可能是原因,当gradle满足所有依赖项时,但org.junit.Test仍未找到。至少这对我来说是一个原因。它不在“发布”构建类型中工作,但在“调试”中工作。

回答by hassan

Download (junit-4.12.jar) from https://github.com/junit-team/junit4/wiki/Download-and-Installand copy it in your libs folder:(yourProjectFolder/app/libs)

https://github.com/junit-team/junit4/wiki/Download-and-Install下载 (junit-4.12.jar) 并将其复制到您的 libs 文件夹中:( yourProjectFolder/app/libs)

and then in build.gradle(Module: app) use this codes:

然后在 build.gradle( Module: app) 中使用以下代码:

dependencies {
...
compile files('libs/junit-4.12.jar')
...
}

then rebuild your project.

然后重建你的项目。