简体   繁体   English

如何在Maven和Eclipse中正确使用范围“测试”和junit

[英]How to use scope “test” and junit correctly with maven and eclipse

I am trying to understand what I am doing wrong with junit-tests in Maven with eclipse. 我试图了解我在Eclipse中使用Maven进行junit-tests所做的错误。 Mainly I have been just following this "getting started guide" for Maven: https://spring.io/guides/gs/maven/ 我主要是一直在遵循Maven的“入门指南”: https : //spring.io/guides/gs/maven/

But for me it is not completely working like that when it comes to junit. 但是对我而言,在junit方面,它并不能完全正常工作。

To follow the exact folder-structure from the example I have named my packages under src in eclipse: main.java.hello test.java.hello 为了遵循示例中的确切文件夹结构,我在Eclipse中的src下命名了我的包:main.java.hello test.java.hello

My test class GreeterTest.java is in the package test.java.hello and has this content: 我的测试类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"));
    }

}

In my pom.xml you find the dependency: 在我的pom.xml中,您可以找到依赖项:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

Unfortunately eclipse sais "the import org.junit cannot be resolved." 不幸的是,eclipse说“导入org.junit无法解决”。 If change the scope of the dependency from test to compile I do not get this error message, but that would be not the way how the scope is intended to be used, is it? 如果将依赖项的范围从测试更改为编译,我不会收到此错误消息,但这不是打算使用范围的方式,是吗?

What do I need to change that also eclipse understands, that this class is a test-class and therefore all dependencies are actually available? 我需要改变一下eclipse也理解的一点,即该类是测试类,因此所有依赖项实际上都可用吗?

Best,Nils 最佳尼尔斯

The issue appears to be related to where Eclipse thinks your source tree starts. 这个问题似乎与Eclipse认为您的源代码树开始的位置有关。 You have put your code in src/main/java/hello and src/main/test/hello but Eclipse thinks the source tree starts at src . 您已经将代码放在src/main/java/hellosrc/main/test/hello但是Eclipse认为源代码树从src开始。 So it thinks the other folders are packages and has given your classes package names like main.java.hello.Greeter . 因此,它认为其他文件夹是软件包,并为您的类提供了软件包名称,例如main.java.hello.Greeter

The quickest way to fix this is to run on the command line: 解决此问题的最快方法是在命令行上运行:

mvn eclipse:eclipse

which uses maven to fix your eclipse projects. 使用Maven修复您的Eclipse项目。 It will automatically set the source root to be the correct values. 它将自动将源根设置为正确的值。 When it completes, right click on the project and select Refresh to see the updated project. 完成后,右键单击该项目,然后选择“ Refresh以查看更新的项目。

To fix this manually, you can right click on the project and choose Build Path > Configure Build Path and, in the Source tab on the right, ensure that the entire src/main/java (right up to java !) is included as the source folder. 要手动修复此问题,您可以右键单击该项目,然后选择“ Build Path > Configure Build Path ,然后在右侧的“ Source选项卡中,确保将整个src/main/java (最多包含java !)作为源文件夹。 Do this by clicking Add Folder and selecting java in the tree under src - main . 通过单击Add Folder并在src - main下的树中选择java来执行此操作。 Do the same for src/main/test . src/main/test做同样的事情。 Usually maven projects include src/main/resources as a source folder too. 通常,maven项目也将src/main/resources作为源文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM