简体   繁体   中英

How to debug breakpoints in multithreaded applications tested with JUnit?

Testing a multithreaded application with JUnit5, the automated tests run just fine. But in manual debugging, the environment misbehaves, and I would like to know how to get it to act as it should.


IntelliJ (using the Java VM + its debugger) has two general options to suspend on breakpoints: either "All" = all threads, or "Thread" = the current thread only.

(Which thread is "the current thread" may be unclear and causing issues, but that's been discussed separately, see other issues here on stackOverflow.)

Wanted behaviour:

  • a breakpoint to suspend a specific thread, and the JUnit test environment , which is part of the IDE debugging.

Actual behaviour:

  • either all threads are suspended, even those which should be running
  • or only a single thread is suspended, and JUnit @AfterEach/@AfterAll annotations kill the resources currently debugged, terminating the threads that should be running, etc.

How to achieve the Wanted behaviour?


(Environment: AdoptOpenJDK8, AdoptOpenJDK11 in Java 9 mode, IntelliJ 2020.3, JUnit-Jupiter 5.7.0)

You can use two breakpoints. The one you're really interested in and another one in your unit test (somewhere between calling the code under test and cleaning up). Set both to only suspend the current thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM