Running JUnit5 Tests with Maven

Learn to run JUnit 5 tests with Maven.

Apache Maven is a project management tool that helps us build projects, manage dependencies, create project reports, and generate documentation.

Maven build life cycle

To build our project, Maven follows a build lifecycle that goes through the following phases.

  • Validate: Verifies whether the pom.xml is valid.
  • Compile: Compiles the source code.
  • Test: Runs the unit tests inside the project.
  • Package: Packages the source code into an artifact.
  • Verify: Runs integration tests, and verifies the validity package.
  • Install: Installs the package into the local repository.
  • Deploy: Deploys the package into the remote repository.

We’re mainly interested in the test phase of the lifecycle. This is where the unit tests inside our project are executed.

Command to execute test

Maven defines a goal for each phase in the build lifecycle.

We use the following command to execute the tests inside our project.

mvn test

Maven Surefire Plugin

To run the above command and execute the tests, we add maven-surefire-plugin to the pom.xml file.

The plugin configuration looks like:

Get hands-on with 1200+ tech skills courses.