I’m using Eclipse Kepler and Maven 3.2.3. I have imported my projects using the M2Eclipse plugin. Normally, when I run a JUnit test on the command line, like
mvn clean test -Dtest=MyJunitTest
there are some things that run in the “process-resources” and “process-classes” phases before my test is executed. In Eclipse, when I open the JUnit test file in an editor, right click the class name (e.g. “MyJUnitTest”) and right click on “Run As” and “JUnit Test”, those phases do not seem to be getting run. So my question is, how can I make right-clicking on my Junit Test and selecting “Run As” -> “JUnit Test” behave as if I had typed “mvn clean test -Dtest=MyJunitTest” on the command line?
I prepared the following (inspired by user3254289's answer):
Extended the projects POM SO-30767338/pom.xml
Implemented SO-30767338/src/test/java/MavenTestRunner.java
Created a Run Configuration according to {workspace}/.metadata/.plugins/org.eclipse.debug.core/.launches/Test selected test unit with Maven.launch
Note the essential part in VM arguments:
After I had all this set up I selected a JUnit class (not
MavenTestRunner,
of course :-) and invoked the run configuration created above via the Run icon's () dropdown list. Voilà! Like a charm!The only shortcoming is that it can't be invoked via a resource's context menu. Anyone keen to write an Eclipse plugin?
Maven and JUnit are two completely different tools, each with a different build - run path and each with different requirements. Just because Maven can utilize JUnit as part of it's toolchain, does not mean they are the same!
You apparently made your project dependent on the Maven build path. It would be extremely difficult, or maybe outright not possible, to make JUnit follow that path.
In Eclipse, with the m2e plugin, it is trivial to create custom runs, using the Run As > Maven build. Documentation is available if you need it.
This "Maven build" option is available only from top level of your project, where your pom is located. You will then have to specify all your options, such as
-Dtest=MyJunitTest
.