Running a Single Unit Test with Maven

I often find myself needing to run a single unit test for a particular project from the command line in my terminal window, rather than all of the tests. In this blog post, I’ll show you how to do just that: run a specific unit test using Maven from the command line.

For video tutorials on how to do Unit testing and Integration testing of Java applications, please have a look at my video course “Testing Java code with JUnit and Mockito“.

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 run all tests in a single test class, do these two steps:

  1. Open a terminal window and change the 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

If you want to run only specific tests in your test class that match a certain pattern, you can do so by using a command like the one below. In this example, the command will run all the tests in the test class that begin with the string ‘testCreate’.

mvn -Dtest=UsersServiceImpl#testCreate* test

POM.xml Dependencies

If you want to run unit tests for your project using the Maven command, you’ll need to add the following dependencies to the <build> section of your project’s 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 running 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.