【发布时间】:2017-07-29 09:16:30
【问题描述】:
我正在尝试使用 gradle 实现一个简单的 junit 测试,并在运行 gradle test 时遇到以下问题:
: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。
【问题讨论】:
标签: gradle junit build.gradle