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

Agg*_*sor 21 java junit android unit-testing

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

我正在安装Android工作室进行单元测试.

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

我做了一个随机测试课程: 在此输入图像描述

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

错误:包org.junit不存在

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

dependencies {
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 29

我将TestCompile更改为androidTestCompile,它没有问题.

testCompile 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)

androidTestCompile 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)

  • testCompile用于`test`目录,androidTestCompile用于`androidTest`目录 (2认同)

小智 5

添加此依赖项来解决您的问题

testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)


Agg*_*sor 2

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

手动添加 jar解决了问题。