I am trying to understand what I am doing wrong with junit-tests in Maven with eclipse. Mainly I have been just following this "getting started guide" for Maven: https://spring.io/guides/gs/maven/
But for me it is not completely working like that when it comes to 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
My test class GreeterTest.java is in the package test.java.hello and has this content:
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:
<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." 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?
Best,Nils
you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests. Hint: By default surefire looks for files with *Test.
The Maven Surefire Plugin 2.22. 0 (or newer) provides native support for JUnit 5. If we want to use the native JUnit 5 support of the Maven Surefire Plugin, we must ensure that at least one test engine implementation is found from the classpath. We can run our unit tests by using the command: mvn clean test.
The issue appears to be related to where Eclipse thinks your source tree starts. You have put your code in src/main/java/hello
and src/main/test/hello
but Eclipse thinks the source tree starts at src
. So it thinks the other folders are packages and has given your classes package names like 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. 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.
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. Do this by clicking Add Folder
and selecting java
in the tree under src - main
. Do the same for src/main/test
. Usually maven projects include src/main/resources
as a source folder too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With