JUnit and Maven in Eclipse

How to add JUnit tests to Maven application in Eclipse, where to find JUnit test results and how to skip tests.

JUnit and Maven, how to skip tests

 

Add inside pom.xml this dependency:

 

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

 

This dependency will have scope "test", which means, that it won't be packaged and will be available only during test phase.

 

And you are done. Now you can create your JUnit tests.

 

Create them inside src/test/java, so that they are separated from your classes. Inside src/test/resources should be non-java files like properties, XML files etc., which are used by tests.

 

To run tests, run "mvn test" or any other Maven phase that uses this phase like "package".

 

If some test fails, you will see BUILD FAILURE message. If you want to see detailed information about each test, see directory target/surefire-reports. There are JUnit test results.

 

Beware that all tests are run before packaging your application. If any of them fails, Maven won't package your application. You can skip tests using: -Dmaven.test.skip=true