【发布时间】:2019-01-19 23:53:57
【问题描述】:
我试图了解我在 Maven 中使用 eclipse 的 junit-tests 做错了什么。主要是我一直在关注 Maven 的“入门指南”: https://spring.io/guides/gs/maven/
但对我来说,在 junit 方面并不完全如此。
为了遵循示例中的确切文件夹结构,我在 Eclipse 中将我的包命名为 src: main.java.hello test.java.hello
我的测试类 GreeterTest.java 位于 test.java.hello 包中,内容如下:
package test.java.hello;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import org.junit.Test;
import main.java.hello.Greeter;
public class GreeterTest {
private Greeter greeter = new Greeter();
@Test
public void greeterSaysHello() {
assertThat(greeter.sayHello(), containsString("Hello"));
}
}
在我的 pom.xml 中你找到了依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
不幸的是,eclipse 说“无法解析导入 org.junit。” 如果将依赖项的范围从 test 更改为 compile,我不会收到此错误消息,但这不是预期使用范围的方式,是吗?
我需要更改哪些 eclipse 也知道,这个类是一个测试类,因此所有依赖项实际上都是可用的?
最好的,尼尔斯
【问题讨论】:
-
您的班级有包
test.java.Hello的事实表明您的项目布局可能不正确。请显示您的项目的树结构,特别是您的测试的位置。 -
确保测试代码不在文件夹
src/main/java,而是在文件夹src/test/java(见this short video)。也许您还必须右键单击该项目并选择 Maven > Update Projects...。如果这没有帮助,请添加显示文件位置的 Package Explorer 视图的屏幕截图。