gradle compileJava错误:包org.junit不存在
我试图使用gradle实现一个简单的junit测试,在运行gradle compileJava错误:包org.junit不存在,gradle,junit,build.gradle,Gradle,Junit,Build.gradle,我试图使用gradle实现一个简单的junit测试,在运行gradle测试时遇到了以下问题: :compileJava /Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: package org.junit does not exist import static org.junit.Assert.assertEquals; ^ /Users/wogslan
gradle测试时遇到了以下问题:
:compileJava
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:2: error: package org.junit does not exist
import org.junit.Test;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:5: error: cannot find symbol
@Test
^
symbol: class Test
location: class CalculatorTest
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:9: error: cannot find symbol
assertEquals(6, sum);
^
symbol: method assertEquals(int,int)
location: class CalculatorTest
5 errors
:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details.
所以我得到了这个build.gradle
文件:
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.12'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
和CalculatorTest.java
:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}
但我不明白,当我将junit包含在依赖项中时,为什么它找不到它。尝试添加
repositories {
maven { url 'http://repo1.maven.org/maven2' }
直接在您的构建脚本{in build.gradle下,因此显然我需要添加一个编译
依赖项,然后还声明存储库
。成功运行测试的新build.gradle
:
apply plugin: 'java'
repositories { jcenter() }
dependencies {
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
我知道这很旧,但我最近遇到过。您应该能够更改srcdir以使用gradle进行测试,并且还可以进行单元测试。如果您需要src/*结构,那么只需将所有测试放在test/*中即可
您可能遇到的问题是,如果您将测试包括在主/java代码文件夹中,它将在该阶段尝试编译这些测试。将它们移到src文件夹之外,并相应地更新srcdir结构,它将按预期工作
apply plugin: 'java'
repositories { jcenter() }
dependencies {
testCompile 'junit:junit:4.12'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
test {
java {
srcDir 'test'
}
}
}
我在最新的android版本中也遇到了同样的问题,我已经用下面的代码解决了。希望你能得到帮助
dependencies {
testImplementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'
}
在build.gradle文件中添加以下依赖项:
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'
这就解决了我的问题。希望这也能对你起作用。因此,缺少buildscript{
可能是问题所在?上面的build.gradle
就是我的全部。请看:如果你更改了sourceset
,gradle就不能进行单元测试了,你必须将junit编译到生产APK中。