1

I have a project made of several Maven projects, where the tests are in separated modules. E.g:

com.xyz.abc.feature.model
com.xyz.abc.feature.model.test

The tests are JUnit tests, and I have been running them from the command line with mvn install. I want to step through the code in a specific test, so I have imported the project into Eclipse as a Maven Project. I tried putting a breakpoint in the unit test, Eclipse does not stop at it when executing the mvn install goal.

How can I debug a specific test in this situation?

3
  • The first issue is having separated modules. Unit tests belong to their appropriate module which means you have the production code in src/main/java/PACKAGE and the appropriate unit tests in src/test/java/PACKAGE. Those tests will be run by maven-surefire-plugin or you can simply start them from Eclipse without doing mvn install on command line...
    – khmarbaise
    Commented Sep 30, 2018 at 14:19
  • Changing the structure of the tests is not an option here.
    – Renoa
    Commented Sep 30, 2018 at 14:39
  • Why not? Its the convention to have them in that way. So the question is why you don't follow the convention? What is the reason?
    – khmarbaise
    Commented Sep 30, 2018 at 15:10

3 Answers 3

4

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:

  1. 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

  1. 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!

1
  • 1
    Unfortunately this did not work for me. mvnDebug waited for the debugger, continued and ran to the end ignoring my breakpoint. Commented Apr 3, 2023 at 13:39
1

There are two main approaches here.

First, you could just execute the test from Eclipse and debug it like you would debug any other Java program. This is probably the easiest approach, but may not be accurate enough if there are differences between the Maven execution and the execution in Eclipse (e.g., environment variables, test order, etc.).

The second approach is to run mvnDebug instead of mvn this is essentialy mvn with a remote debug port open. You can launch the build, connect Eclipse to the remote debugging port and debug from there.

4
  • The second part will debug Maven itself which I don't think the original post will really do...
    – khmarbaise
    Commented Sep 30, 2018 at 14:19
  • @khmarbaise Using mvnDebug will allow you to debug any java code in that process. If you place your breakpoint in the test's code, you'll debug the test.
    – Mureinik
    Commented Sep 30, 2018 at 14:20
  • That presumes that you have only a single process which can't be predicted. If you have configured surefire/failsafe to fork this will not work..and part from that it is overengineered cause to debug a test can be done from the IDE...much simpler.
    – khmarbaise
    Commented Sep 30, 2018 at 14:27
  • @Mureinik , could you add some more detail please? I am not familiar with Eclipse so I'd appreciate more detailed information on what steps to follow.
    – Renoa
    Commented Sep 30, 2018 at 14:41
1

Just open the test in eclipse, double-click on the test name to select it (actually just clicking in the test-name to put the cursor in it should be enough, or you can e.g. select it in the outline view), then right-click and choose Debug As > JUnit Test

1
  • It's so easy. You need more upvotes. Commented Apr 3, 2023 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.