Open In App

Difference Between “mvn verify” and “mvn test”

Last Updated : 21 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The mvn verify and mvn test commands are used in Maven for build automation. Maven is one of the most popular build automation tools. In this article, we will learn about mvn verify vs mvn test.

mvn verify

The mvn verify command in Maven is used to execute the verify phase, which occurs later in the build lifecycle than the test phase. This command runs not only unit tests but also any integration tests, and it checks to ensure that the package is valid and meets quality requirements.

Working of mvn verify:

  • Compiles the code: It compiles the source code.
  • Compiles the tests: It compiles the test source code.
  • Runs the tests: It runs the unit tests.
  • Additional checks: It performs any checks defined in the lifecycle, such as integration tests and any other checks defined by the project’s configuration.

Syntax:

mvn verify

mvn test:

The mvn test command runs in the test phase of the Maven build lifecycle. This phase is designed specifically to execute the unit tests that are present in the project.

Working of mvn test:

  • Compiles the code: Maven compiles the main source code.
  • Compiles the tests: Maven compiles the test source code.
  • Runs the tests: Maven runs the tests using a test framework such as JUnit or TestNG.

Syntax:

mvn test

Tools And Technologies are used:

  • Java programming
  • Maven
  • Spring Tool Suite
  • Commend Prompt

Note: Before going to this article you should knowledge on maven project creation and It's build life cycle then only you can understand this concept in right way. And you must haven maven in your local system.

Implementation of mvn verify and mvn test

Here is a step-by-step example of using "mvn verify" and "mvn test" on a sample Maven project:

Step 1: Create a Maven Project

Create a Maven project using any IDE, such as Spring Tool Suite, and include essential Maven dependencies in the pom.xml file.

Dependencies:

Below commends are available in pom.xml file once project is successfully created.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


Project Folder Structure:

Folder Structure


Step 2: Write Sample Java Code

Now, write a sample Java code in the test class to execute unit tests.

Java
package com.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MavencommendsApplicationTests {

    @Test
    void contextLoads() {
    }

    @Test
    public void testAddition() {
        assertEquals(2, 1 + 1);
    }

}

Step 3: Navigate to Project Folder

Now, open the command prompt, then redirect into project folder by using cd command.

Navigate to Project Folder


Step 4: Execute mvn verify Command

Now, type the mvn verify command and click on enter then you will get below result.

Output:

mvn verify command execution


Maven Build Successfully:

mvn verify executed


Step 5: Run mvn test Command

Now, check the mvn test commend to execute unit tests during the test phase.

Output:

mvn test command execution


Maven Build Successfully:

mvn test command executed


Difference Between mvn verify and mvn test

Below we provide difference between mvn verify and mvn test for understanding the concept in better way.

Feature

mvn test

mvn verify

Purpose

Runs unit tests.

Runs unit tests, integration tests, and additional checks.

Build Life cycle Phase

Executes up to the test phase.

Executes up to the verify phase.

Compilation

Compiles main and test code.

Compiles main and test code.

Unit Tests

Runs unit tests.

Runs unit tests.

Integration Tests

Does not run integration tests.

Runs integration tests.

Additional Verification

No additional verification steps.

Performs additional verification steps.

Typical Use Case

Quick verification of code changes.

Comprehensive validation before deployment.

Plugins Used

maven-surefire-plugin

maven-surefire-plugin, maven-failsafe-plugin

Configuration Scope

Focuses on unit tests located in src/test/java

Includes integration tests typically in src/it/java

Feedback Speed

Faster, since it runs fewer checks.

Slower, due to more extensive checks and tests.

Command

mvn test

mvn verify



Article Tags :

Explore