Debug Maven Surefire unit tests by running them in JDWP "wait for debugger" mode (-Dmaven.surefire.debug) and attaching to the forked test JVM using jdb (preferred for CLI/agent debugging),...
Run Maven Surefire tests suspended in JDWP so you can attach a debugger and step through the code.
In headless/agent environments, attach with jdb (the JDK’s command-line debugger).
mvn test with Surefire in debug mode via -Dmaven.surefire.debug.suspend=y, meaning:This is important: you do not attach to the mvn process itself; you attach to the forked JVM running the tests.
.codex/skills/debug-surefire/scripts/debug-surefire.sh --test-class MyTest#):.codex/skills/debug-surefire/scripts/debug-surefire.sh --test 'MyTest#shouldDoThing'.codex/skills/debug-surefire/scripts/debug-surefire.sh --module core/sail/shacl --test-class ShaclSailTest.codex/skills/debug-surefire/scripts/debug-surefire.sh --module rdf4j-sail-shacl --test 'ShaclSailTest#testSomething'When the forked JVM is ready, you should see something like:
Listening for transport dt_socket at address: 55005jdb (preferred)In a second terminal, attach to the printed port:
jdb -attach 55005jdb -attach localhost:55005If you need source listing inside jdb, provide a sourcepath up front:
jdb -sourcepath module/src/main/java:module/src/test/java -attach 55005Once you’re at the jdb prompt, set a breakpoint (or exception catch) and continue:
stop in com.example.MyTest.shouldDoThingcatch uncaught java.lang.AssertionErrorcontTip: right after attaching to a just-started suspended JVM, jdb may show:
No frames on the current call stackThat’s normal. The JVM hasn’t executed into any Java frames yet. Set breakpoints first, then cont.
-Pquick into the repo-local .m2_repo) and then runs mvn test with Surefire in debug mode.SUREFIRE_DEBUG_PORT=8000 to change the port (default: 55005).--no-offline / --online if offline (-o) resolution fails.-- is passed to Maven, e.g.:.codex/skills/debug-surefire/scripts/debug-surefire.sh --test-class MyTest -- -DtrimStackTrace=false -DfailIfNoTests=false -DforkCount=1 -DreuseForks=falseThis section is optimized for quickly getting signal from a failing unit test without drowning in framework/JDK internals.
Start / resume
cont — continue execution from current stop/breakpointrun — start execution when jdb launched the VM (less relevant when you used -attach)Breakpoints
stop in <class>.<method> — break on method entrystop at <class>:<line> — break at a specific linestop — list all breakpointsclear <class>.<method> / clear <class>:<line> — remove a breakpointclear — list breakpoints (same idea as stop listing)Stepping
next — step over calls (line-level)step — step into calls (line-level)step up — run until current method returnsstepi — step one bytecode instruction (rarely needed)Threads / stacks
threads — list threadsthread <id> — select default threadwhere — stack trace for current threadwhere all — stack traces for all threadsup / down — move the current frame up/down the stackInspect state
locals — print locals in current frameprint <expr> — evaluate/print an expression (same as eval)dump <expr> — more complete object dumpset <lvalue> = <expr> — mutate a variable/field (use sparingly)Source
list — show source around current linelist <line> or list <method> — show a specific regionuse <path1>:<path2> (aka sourcepath) — set where jdb looks for sourcesExceptions
catch uncaught <exception> — break when an uncaught exception occurscatch caught <exception> — break when a caught exception occurscatch all <exception> — break for both caught+uncaughtignore ... — undo a catchReduce noise
exclude <pattern>,<pattern>,... — don’t report step/method events for matching classesexclude none — clear exclusionsAutomation
monitor <command> — run a command every time the program stops (e.g., monitor where)read <file> — execute commands from a file!! — repeat last command<n> <command> — repeat command n times (e.g., 10 next)Right after connecting, set exclusions to avoid stepping into JDK/framework code:
exclude java.*,javax.*,jdk.*,sun.*,com.sun.*,org.junit.*,org.junit.jupiter.*,org.assertj.*,org.hamcrest.*,org.mockito.*,org.apache.maven.*You can always clear this later:
exclude noneCommon breakpoint patterns:
Break at the failing test method:
stop in com.example.MyTest.shouldDoThingBreak inside the code under test:
stop in com.example.service.FooService.doWorkBreak at a specific suspicious line:
stop at com.example.service.FooService:123If the method is overloaded, specify argument types:
stop in com.example.FooService.doWork(int,java.lang.String)Note: jdb supports deferred breakpoints. If the class isn’t loaded yet, it will still accept the breakpoint and activate it when the class loads.
For unit tests, breaking on the thrown assertion/error is often faster than guessing a line.
Useful catches:
Classic Java assertions / many test failures:
catch uncaught java.lang.AssertionErrorCommon in JUnit 5 assertions:
catch uncaught org.opentest4j.AssertionFailedErrorNPE hunting:
catch uncaught java.lang.NullPointerExceptionYou can remove a catch with ignore:
ignore uncaught java.lang.AssertionErrorTip: catch all java.lang.Throwable is the nuclear option. It works, but it can get loud.
Once breakpoints/catches are set:
contWhen you hit a breakpoint:
where to see the call stacklist to see nearby sourcelocals to see local variablesprint someVar or print this.someField to inspectnext to step over, step to step intoSurefire + JUnit can spin up multiple threads (and Maven itself has its own). When in doubt:
threadswhere allThen pick the thread that’s in your test/code-under-test:
thread <id>whereSurefire forking and parallelism can make debugging confusing. If you see multiple JVMs / inconsistent behavior, pass Maven flags to keep it single and fresh:
-DforkCount=1 -DreuseForks=falseExample:
.codex/skills/debug-surefire/scripts/debug-surefire.sh --test 'MyTest#shouldDoThing' -- -DforkCount=1 -DreuseForks=falseBy default, jdb breakpoints suspend all threads, which can create deadlocks in concurrent tests. You can tell jdb to suspend only the thread that hits the breakpoint:
stop thread in com.example.concurrent.Worker.run(You can also target a specific thread id with stop thread <thread_id> in ... if needed.)
jdb will execute startup commands from jdb.ini or .jdbrc in either user.home or user.dir.
This is handy for keeping your default exclusions/catches consistent, e.g.:
exclude java.*,javax.*,jdk.*,sun.*,com.sun.*,org.junit.*,org.assertj.*catch uncaught java.lang.AssertionErrorYou can also keep a project-local command file and load it on demand:
read .codex/skills/debug-surefire/jdb.cmdsjdb can’t connect
jdb -attach.Breakpoints don’t hit
classes to see what’s loaded.stop at <class>:<line> to avoid signature mismatch.list can’t find source
use module/src/main/java:module/src/test/java-sourcepath ... from the start.