[英]How to use scope “test” and junit correctly with maven and eclipse
我试图了解我在Eclipse中使用Maven进行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无法解决”。 如果将依赖项的范围从测试更改为编译,我不会收到此错误消息,但这不是打算使用范围的方式,是吗?
我需要改变一下eclipse也理解的一点,即该类是测试类,因此所有依赖项实际上都可用吗?
最佳尼尔斯
这个问题似乎与Eclipse认为您的源代码树开始的位置有关。 您已经将代码放在src/main/java/hello
和src/main/test/hello
但是Eclipse认为源代码树从src
开始。 因此,它认为其他文件夹是软件包,并为您的类提供了软件包名称,例如main.java.hello.Greeter
。
解决此问题的最快方法是在命令行上运行:
mvn eclipse:eclipse
使用Maven修复您的Eclipse项目。 它将自动将源根设置为正确的值。 完成后,右键单击该项目,然后选择“ Refresh
以查看更新的项目。
要手动修复此问题,您可以右键单击该项目,然后选择“ Build Path > Configure Build Path
,然后在右侧的“ Source
选项卡中,确保将整个src/main/java
(最多包含java
!)作为源文件夹。 通过单击Add Folder
并在src - main
下的树中选择java
来执行此操作。 对src/main/test
做同样的事情。 通常,maven项目也将src/main/resources
作为源文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.