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!

0
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

This is going to force me back to NetBeans. The default behavior for right click -> Run needs to be Maven for Maven projects. Currently it detects that the file is a JUnit test and creates a JUnit run configuration. Ok. Why not also detect that the JUnit test is part of a Maven project and create a Maven run configuration instead? E.g.,

-Dtest=MyTestClass test 
0
Avatar
Permanently deleted user

And...

-Dtest=MyTestClass#myTest test

...for individual test methods. Kinda weird to make a run configuration but I can live with that IF it was using the right run tool.. Maven.

0

Thank you for your feedback. Currently auto-creation of Maven Run/Debug Configuration is working only from Maven tool window. I have created a request you may vote and follow: https://youtrack.jetbrains.com/issue/IDEA-244632 Thanks.

Please also consider using Run Anything action to quickly launch any maven command including test.

0

We are currently evaluating to switch from Netbeans to IntelliJ. This is also a big problem for us, as we have lots of maven projects with tests, and it is very tedious to run them by hand (also with Run Anything).

0

Nicolai Ehemann I've added a private note to the related issue in YouTrack. Please follow IDEA-244632, for updates.

Here you can find more information about how to run a single test in a Maven project in the current implementation - https://www.jetbrains.com/help/idea/work-with-tests-in-maven.html#run_single_test.

0

请先登录再写评论。