Is there a way to check in JUnit code if the debugger is attached to the current test execution?
In .NET/C# i know this is possible with Debugger.IsAttached
.
The use case would be to change or completly disable test timeouts when the debugger is attached because this is pretty annoying if you only have about 15 seconds (the defined timeout) for debugging a test.
It is not a 100% fail-safe approach but you can check if the Java Debug Wire Protocol (JDWP) is active which is used by the debugger to connect to a JVM. This can be done by checking for input arguments to the JVM as for example in:
boolean isDebug() {
for(String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
if(arg.contains("jdwp=")) {
return true;
}
}
return false;
}
It might however return a false positive if anybody else named something jdwp
or if the protocol was used for something else. Also, the command might change in the future what makes this hardly fail-safe. This approach is of course not JUnit specific but as JUnit does not have any privileges within a JVM, this is only natural.