Run Single Test Case with Maven

I cannot figure out how to configure IntelliJ's convenient "run from here" style test runner to work seemlessly with Maven.

I understand that I can execute all of a maven project's unit/integration tests by running the Maven Build for a given phase under Maven Projects, but I want finer granularity control than that.

Without Maven, you can right-click on a test class/method in IntelliJ and select "Run..." (or "Debug..." for that matter) and it automatically compiles and runs just what you've selected. When I try to do this with a Maven project, it fails because it tries to build using IntelliJ (and our build is much too complicated for that to work).

How can I configure the handy "run unit test from here" functionality to run the maven test/integration-test build phase for the containing project by default? Manually creating a Run/Debug Configuration every single time I want to test a method/class, for each method/class I want to test, is beyond tedious. I usually just give up and run my tests with maven directly from the command line.

Any help/suggestions are greatly appreciated.

Thanks!

3 comments

Shy of writing a custom plug-in, I do not know of a way to instantly run a single test via maven. However, you can configure the IDEA Unit Test runner (i.e. the run.debug configuration) to run a maven build (rather than an IDEA internal build) prior to running. To make this default for a project, open the run/debug edit configurations dialog (Run > Edit Configurations). Expand the "Defaults" section. Select the JUnit (or TestNG) node. In the "Run before launch" section, remove the "Make" option and add a "Run Maven Goal" definition to run the  test-compile goal. If you are running IDEA 12, you can define multiple "Run before Launch" items so you can (if desired) configure it to run the  clean goal before the test-compile goal. After doing such, any time you run a unit test "from here", it will create the run/debug configuration to run the maven test-compile prior to running the test,

If you want to make that the default behavior for all future projects, close all projects so you are on the Welcome screen. Go to Configure > Template Project >  Run Configurations. (For versions prior to 12, go to the Welcome screen and select Run > Edit Configurations). Make the settings there to make them the default settings for all future projects,

0

Thanks Mark, that's something at least, but since we have many maven submodules, having to set up the default JUnit Run Configuration to invoke the test-compile goal on the root pom isn't exactly efficient.

0

netbeans does this nicely

hard to believe that idea wont support this

most java projects nowadays are maven based and intellij runner is simply not smart enough to respect the same lifecycle that maven does - if you do any smart tricks with maven/ant or use features like serenity/cucumber, your test starts failing left and right, its super annoying

1

Please sign in to leave a comment.