Running a Single Unit Test with Maven

Often I need to open a certain project and run one of its unit tests from a command line in my terminal window. Not all unit tests but to run a single, specific unit test only. And with this blog post, I am going to share with you how to do it. How to run a single unit test with Maven from a command line.

Let’s say I have a Test class with the name UsersServiceImplTest.

Run All Test Methods

It is very easy to run all test methods in a project with mvn test command. There is no more you need to do.

  1. Open a terminal window,
  2. Change directory to your Spring Boot project,
  3. Run the mvn test command.

Like so:

mvn test

Run All Tests in a Class

To all tests in a single test class do these two steps:

  1. Open a terminal window and change directory to your Maven project. You should be in a directory that contains pom.xml file,
  2. Run the below command:
    mvn -Dtest=UsersServiceImplTest test

    where the UsersServiceImplTest is a Test class with test methods.

Run a Single Unit Test

To run a single unit test, do the following:

  1. Open a terminal window and change directory to your Maven project. You should be in a directory that contains pom.xml file,
  2. Run the below command:
    mvn -Dtest=UsersServiceImpl#testCreateUser test

    where the UsersServiceImplTest is a test class and the testCreateUser is a name of a method you are testing.

Run Tests that Match a Pattern

You can also run selected tests in your test class which matches a pattern. For example, the command below will run all tests in a test class that starts with “testCreate”.

mvn -Dtest=UsersServiceImpl#testCreate* test

POM.xml Dependencies

For your project to support running Unit tests with maven command, add the following dependencies to a <build> section of the pom.xml file.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

I hope this very short maven tutorial on how to run a single unit test was of some value to you. If you would like to learn Maven by watching a step by step video lessons, please check the below links.


3 Comments on "Running a Single Unit Test with Maven"


  1. Thanks Sergey. I think that the spring-boot-maven-plugin is needed only if you are working with Spring Boot and want to hook into the integration-tests maven lifecycle phase.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!