gpt4 book ai didi

gradle compileJava 错误 : package org. junit 不存在

转载 作者:行者123 更新时间:2023-12-03 10:51:46 25 4
gpt4 key购买 nike

我正在尝试使用 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。

最佳答案

所以显然我需要添加一个 compile依赖,然后还声明 repositories .我的新 build.gradle成功运行测试:

apply plugin: 'java'

repositories { jcenter() }
dependencies {
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
}

sourceSets {
main {
java {
srcDir 'src'
}
}
}

关于gradle compileJava 错误 : package org. junit 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com