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

时间:2016-05-05 23:25:10

标签: java android unit-testing junit

更新:这是一个错误并且已经报告,请注明: https://code.google.com/p/android/issues/detail?id=209832&thanks=209832&ts=1463161330

我在Android studio上设置单元测试。

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

我做了一个随机测试课程: 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));
    }
}

然而,当我去测试时,我得到了:

  

错误:包org.junit不存在

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

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

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

在我开始测试时,你能想到我的本地单元测试没有找到junit文件的原因吗?

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

7 个答案:

答案 0 :(得分:27)

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

testCompile 'junit:junit:4.12'

androidTestCompile 'junit:junit:4.12'

答案 1 :(得分:2)

你应该检查的一些事情 -

  • 您是否在构建变体下选择了单元测试和调试?
  • 在单元测试的运行/调试配置中,您的工作目录是否设置为$ MODULE_DIR $?
  • 您是否通过选择要测试的课程来创建测试,转到导航 - >测试并让Android Studio为您构建测试类?

答案 2 :(得分:2)

看起来Gradle没有做到这一点。

Manually adding the jars解决了这个问题。

答案 3 :(得分:2)

我的测试位于src/test/java文件夹中,test.setRoot('test')添加到sourceSets为我工作。

sourceSets {
    test.setRoot('test')
}

答案 4 :(得分:2)

添加此依赖性以解决您的问题

testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'

答案 5 :(得分:-1)

尝试将Build Variant更改为sudo chmod a+x geckodriver

debug

答案 6 :(得分:-1)

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

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

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

然后重建你的项目。

相关问题