Meanwhile, I found a solution on my own. If you have a similar problem, follow the approach described above using the debug option.
test { debug true }
But make sure that external connections are accepted in the settings after restarting IntelliJ:


Then it connects to the correct JVM and breaks at breakpoints using a remote task:


If you restart IntelliJ, however, with the same option (external connections) enabled, the debugging task may fail due to a blocked port:

So, for some reason, IntelliJ blocks this port after a restart, but you need to enable the setting for the debug task. This is strange, and I donβt think it should behave like this.
In any case, if you disable the setting and reboot, the port will open again. Then enable the option again, do not reboot, just run the Gradle task and the debug task. He will work.
I hope this helps anyone looking for an intermediate solution for debugging JVM applications using Gradle and IntelliJ among the confusing and partially obsolete answers. If anyone has a better or simpler suggestion, feel free to add your answer.
source share