[英]Android Studio 2.1: error: package org.junit does not exist
更新:這是一個錯誤,已被報告,請加星標: https : //code.google.com/p/android/issues/detail?id=209832&thanks=209832&ts=1463161330
我正在 Android Studio 上設置單元測試。
我已閱讀文檔並完全按照指定進行設置。 我將我的測試文件夾設置為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));
}
}
但是,當我去運行我的測試時,我得到:
錯誤:包 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。
我將 TestCompile 更改為 androidTestCompile 並且它沒有問題。
testCompile 'junit:junit:4.12'
到
androidTestCompile 'junit:junit:4.12'
添加此依賴項以解決您的問題
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
你應該檢查的一些事情 -
看起來 Gradle 沒有完成它的工作。
手動添加罐子解決了這個問題。
我的測試在src/test/java
文件夾中,並將test.setRoot('test')
到對我sourceSets
。
sourceSets {
test.setRoot('test')
}
嘗試將 Build Variant 更改為debug
。
View -> Tool Windows -> Build Variants
UPD:這實際上可能是原因,當滿足所有gradle
依賴項時,但仍未找到org.junit.Test
。 至少這對我來說是一個原因。 它不在“發布”構建類型中工作,但在“調試”中工作。
從https://github.com/junit-team/junit4/wiki/Download-and-Install下載 (junit-4.12.jar) 並將其復制到您的 libs 文件夾中:( yourProjectFolder/app/libs
)
然后在 build.gradle( Module: app
) 中使用以下代碼:
dependencies {
...
compile files('libs/junit-4.12.jar')
...
}
然后重建你的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.