Skipping tests in Maven is useful when tests are time-consuming or not required during a particular build phase. Maven provides multiple ways to skip test execution while still allowing the project to build successfully.
- Use -DskipTests to skip running tests
- Use -Dmaven.test.skip=true to skip compiling and running tests
- Can be configured in the POM file or command line
Different Ways to Skip Tests with Maven
We can skip tests in Maven by using below-mentioned ways.
- Using the Command line
- Configuring the pom.xml File
- Using Profiles
Tools and Technologies
- Java Programming
- Java Version 17
- Maven 3.X.X
- JUnit
- Spring Tool Suite
- Command Prompt
Steps to Skip Tests with Maven
Now, we created a simple maven project by using spring initializr with required maven dependencies and we mention them in the below.
Step 1: Create a Maven Project
Create a Maven project by using Spring Tool Suite with basic dependencies.
Dependencies:
<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>
Step 2: Project Structure
Once project created successfully, now observe the project folder structure.

Step 3: pom.xml Configuration
Open the pom.xml file which contains project configuration. This file contains information like project configuration, dependency management and other project related information.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavencommends</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavencommends</name>
<description>Spring Reactive</description>
<properties>
<java.version>17</java.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 4: Skipping Tests Using Command Line
First, we skip tests in Maven by using maven commands. To do this, open command prompt, then redirect to created maven project by using cd command.
Once redirect into project by using commands. Now run the below command to skip tests in the maven project. We can skip tests by adding a -D property to the Maven.
mvn install -Dmaven.test.skip=true
or,
mvn install -DskipTests
Output:

Tests skipped:

Maven Build Successfully:

Step 5: Configure the pom.xml file for Test Skipping
This is another way to skips the tests in the maven project. For this we need add a property in the pom.xml file.
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
Now, run this project as JUnit. After running successfully, open the JUnit panel. Before adding this property we can see some information in the JUnit Panel. After adding we skip the tests that's why no JUnit information is available.
Output:
Before adding test skip property:

After adding test skip property:

Step 6: Using Profiles
We can define a Maven profile to skip tests and activate this profile when needed. This method allows more flexibility as we can choose when to skip tests by activating the profile.
<profile>
<id>skip-tests</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
Add this profile in the pom.xml file and run the project then we get expected output.
Output:
Before adding test skip property:

After adding test skip property:
