Maven SureFire plugin runs the test in a different process by default. That's likely why it wasn't working for you. You would first disable forking by adding this to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>never</forkMode>
</configuration>
</plugin>
</plugins>
</build>
Then follow these steps to debug:
- Launch your test with debug mode:
Go to the folder where your pom.xml is located. Assuming you want to debug up to the 'install' phase, which also covers your test. Run this command
mvnDebug install
This will start a process and wait on default port 8000 (your port could be different, check your output display). My test shows this output:
Listening for transport dt_socket at address: 8000
- Use a remote debugger in Eclipse to connect to the process started in step 1:
Set (or import) projects in Eclipse if you haven't, then go to menu 'Run' -> 'Debug Configuration' -> 'Remote Java Application', and create a new launch configuration. Make sure the port number matches the one displayed in output from step 1. Click 'Debug'. This will connect the Eclipse debugger to your process started in step 1. If you have a breakpoint set in your Eclipse source editor, then it will stop right there.
Some people would say that, by doing so, you are really debug Maven. I tested it and this approach worked just fine. As long as you don't have Maven source code in your Eclipse project, and it stops at the breakpoint set in your own project source code, this just works great!
src/main/java/PACKAGE
and the appropriate unit tests insrc/test/java/PACKAGE
. Those tests will be run by maven-surefire-plugin or you can simply start them from Eclipse without doingmvn install
on command line...