Running Single Integration Test or Method Using Maven

In this blog post we will see how to run single Integration test or method when using Maven as build tool and maven failsafe plugin to run integration tests.

Running Integration Tests

You can use following command to run all the integration tests

mvn verify

Running a Single Test

IDE’s like IntelliJ Idea and Eclipse allow you to run single integration test or method from GUI. Now let’s see how to run single integration test or method from command line using Maven.

During your development phase, you might want to run single integration test case repeatedly instead of running all the test cases every time.

To run the single test with Maven use the -Dit.test=<Test-Name> option like below

mvn verify -Dit.test=EmployeeControllerIT

The value for the it.test parameter is the name of the test class

You can also use pattern to run multiple tests

mvn verify -Dit.test=Employee*IT

You may use multiple names/patterns, separated by commas:

mvn verify -Dit.test=EmployeeControllerIT, Department*IT

You can also use package names or wild cards in pattern matching.

mvn verify -Dtest="dev/fullstackcode/EmployeeControllerIT", "**/Department*IT"

Running Selected Methods in a Single Test Class

Starting from Failsafe plugin version 2.7.3 , we can also run only selected methods from a test case.

mvn verify -Dit.test=EmployeeControllerIT#testMethod

You can also use pattern

mvn verify -Dit.test=EmployeeControllerIT#testMethod*

Starting from Failsafe plugin version 2.19, we can also select multiple methods

mvn test -Dtest=EmployeeControllerTest#testMethod1+testmethod2

Using Multiple Formats in Single Command

From Failsafe Plugin 2.19 in a single command we can set inclusion and exclusion of test cases.

mvn test "-Dtest=???Test, !Unstable*, pkg/**/Ci*leTest.java, *Test#test*One+testTwo?????, #fast*+slowTest"
mvn  verify "-Dtest=Basic*, !%regex[.*.Unstable.*], !%regex[.*.MyTest.class#one.*|two.*], %regex[#fast.*|slow.*]"
  • The file extensions are not mandatory in non-regex patterns , and packages with slash can be used
  • The exclamation mark (!) excludes tests.
  • The character (?) within non-regex pattern replaces one character in file name or path.
  • The regex validates fully qualified class file, and validates test methods separately after (#) however class is optional. 
  •  The regex supports ‘.class’ file extension only

Note : The regex comments, marked by (#) character, are unsupported

Similar Posts