Horje
Running a Single Test or Method With Maven

Running a single test or method using Maven is straightforward and can be accomplished using Maven commands. By default, Maven runs all tests in your project, but you can specify a single test class or even a single test method to execute. In this article, we will learn how to run a single test or method with Maven, providing a related example.

Project Setup:

  • Create a Maven project using the maven-archetype-quickstart. or create by using any IDEs also here we use STS
  • Add JUnit 5 dependencies to pom.xml.
  • Write test cases in a test class.
  • Run a single test class with mvn -Dtest=ClassName test.
  • Run a single test method with mvn -Dtest=ClassName#methodName test

Implementation to Run a Single Test or Method With Maven

Below are the implementation steps to run a Single Test or Method With Maven.

Step 1: Create a Maven Project

First, create a simple maven project by using your favorite tool. Here, we use STS IDE By using required dependencies.

Dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://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>1.8</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>


Project Structure:

Project Folder Structure


Step 2: Write Test Class

Now, navigate to src/test/java folder after this the is a by default Test class is available in that package. Now write your test logic in that test class. This class contains two test methods, testMethod1 and testMethod2.

MavencommendsApplicationTests.java:

Java
package com.app;

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

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

@SpringBootTest
class MavencommendsApplicationTests {

    @Test
    void contextLoads() {
    }

    @Test
    public void testMethod1() {
        assertTrue(true);
    }

    @Test
    public void testMethod2() {
        assertTrue(true);
    }

}


Step 3: Configure Dependencies

Make sure your pom.xml is setup to use JUnit 5. If you don’t have below we provide that dependency copy into your pom.xml then update the Project.

<dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


Step 4: Run Entire Test Class

The following command will run all the test methods in the MavencommendsApplicationTests class. Here we run entire test methods in the test class.

mvn -Dtest=MavencommendsApplicationTests test

Output:

Maven Running


Maven Build Success:

Maven Build Success


Step 5: Run Single Test Method

Now we run a single method in the test class. To run a specific test method, use the following command. Here we run one specific test method from test class by using below command.

mvn -Dtest=MavencommendsApplicationTests#testMethod1 test

Output:

Maven Single Test Method Running


Maven Single Test Method Build Success:

Maven Build Success





Reffered: https://www.geeksforgeeks.org


Advance Java

Related
What is Maven? What is Maven?
Maven - External Dependencies Maven - External Dependencies
Spring Security - CORS Spring Security - CORS
Secure Service Registration with Eureka and Spring Boot Microservices Secure Service Registration with Eureka and Spring Boot Microservices
Optional Dependency in Maven Optional Dependency in Maven

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15