How to use Eclipse debugger for Unit Test

Utilizing a debugger when developing Unit Tests for ONAP can be useful.  The problem is that normal execution using the Maven Surefire plugin runs in its own JVM.  Furthermore, default execution of the test goal results in all tests being run.  I found some handy tips for using the Eclipse debugger and isolating particular tests.

Refs:

  1. http://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html

  2. http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

  3. https://stackoverflow.com/questions/295686/using-the-eclipse-remote-debugger-throws-com-sun-jdi-internalexception

Step-by-step guide

  1. From the command line, invoke mvn test goal but specify the flag to enable remote debugging

    mvn -Dmaven.surefire.debug test

  2. The execution will stop with a message like:

    --------------------------------

    T E S T S

    --------------------------------

    Listening for transport dt_socket at address: 5005



  3. Configure a remote debug job in Eclipse.  There are probably many ways to do this, but I opened Debug Configurations, selected "Remote Java Applications", filled in Name: "remote debug on 5005", Host: localhost, Port: 5005.  This step only needs to be done once.

  4. Set breakpoints of interest, and launch remote debugger created in step 3.

  5. To just run a single test class instead of the whole works, start maven using the -Dtest param.  For example,to only run tests defined in test Class DmaapResourceTest.java:

    mvn -Dtest=DmaapResourceTest -Dmaven.surefire.debug test



  6. I was running on an Ubuntu 16.04 VM instantiated on Oracle VM VirtualBox Manager running on Windows 10, and did experience a problem stepping through some functions.  In Eclipse, I disabled Preferences->Java→"Show method result after a step operation (if supported by the VM; may be slow)" which I found in an unrated answer in Ref 3  above.



Related articles