Execute specific unit test in Maven using Eclipse

Okay, your project has hundreds of unit tests but you only need to execute one or perhaps only all tests from a JUnit test file. Maven allows for that and Eclipse makes it more convenient.

Eclipse JUnit Run Configuration

With Eclipse, you can perform the following:

  1. Run all tests in certain project, package, or source folder
  2. Run all tests in a JUnit file
  3. Run specific test in a JUnit file

All these options are available on the same UI window as shown below. If you are to select option 1 from the above list, choose “Run all tests in the selected project, package or source folder.”

Run Only one Test

The following image shows a JUnit run configuration that executes a test method.

junit-maven-eclipse-24-3-2016 9-43-17 AM

Run All Tests from a Test class

To run all tests from a test class, choose “all methods” from a list shown when “Search…” is clicked.

junit-maven-eclipse-24-3-2016 9-43-56 AM

Eclipse Maven Build Run Configuration

Running tests from Maven is a bit different because you need to specify parameters (aka Goals and etc) as you would when running an application from a command-line prompt.

Run Only one Test

junit-maven-eclipse-24-3-2016 9-57-40 AM

 Run All Tests from a Test class

junit-maven-eclipse-24-3-2016 9-57-16 AM

Download the Maven Project

You may download the Maven project used for this post.


Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert