public class DisableOnDebug extends java.lang.Object implements TestRule
DisableOnDebug
Rule allows you to label certain rules to be
disabled when debugging.
The most illustrative use case is for tests that make use of the
Timeout
rule, when ran in debug mode the test may terminate on
timeout abruptly during debugging. Developers may disable the timeout, or
increase the timeout by making a code change on tests that need debugging and
remember revert the change afterwards or rules such as Timeout
that
may be disabled during debugging may be wrapped in a DisableOnDebug
.
The important benefit of this feature is that you can disable such rules without any making any modifications to your test class to remove them during debugging.
This does nothing to tackle timeouts or time sensitive code under test when debugging and may make this less useful in such circumstances.
Example usage:
public static class DisableTimeoutOnDebugSampleTest { @Rule public TestRule timeout = new DisableOnDebug(new Timeout(20)); @Test public void myTest() { int i = 0; assertEquals(0, i); // suppose you had a break point here to inspect i } }
Modifier and Type | Field and Description |
---|---|
private boolean |
debugging |
private TestRule |
rule |
Constructor and Description |
---|
DisableOnDebug(TestRule rule)
Create a
DisableOnDebug instance with the timeout specified in
milliseconds. |
DisableOnDebug(TestRule rule,
java.util.List<java.lang.String> inputArguments)
Visible for testing purposes only.
|
Modifier and Type | Method and Description |
---|---|
Statement |
apply(Statement base,
Description description)
Modifies the method-running
Statement to implement this
test-running rule. |
boolean |
isDebugging()
Returns
true if the JVM is in debug mode. |
private static boolean |
isDebugging(java.util.List<java.lang.String> arguments)
Parses arguments passed to the runtime environment for debug flags
|
private final TestRule rule
private final boolean debugging
public DisableOnDebug(TestRule rule)
DisableOnDebug
instance with the timeout specified in
milliseconds.rule
- to disable during debuggingDisableOnDebug(TestRule rule, java.util.List<java.lang.String> inputArguments)
rule
- the rule to disable during debugginginputArguments
- arguments provided to the Java runtimepublic Statement apply(Statement base, Description description)
TestRule
Statement
to implement this
test-running rule.apply
in interface TestRule
base
- The Statement
to be modifieddescription
- A Description
of the test implemented in base
base
,
a wrapper around base
, or a completely new Statement.TestRule.apply(Statement, Description)
private static boolean isDebugging(java.util.List<java.lang.String> arguments)
Options specified in:
arguments
- the arguments passed to the runtime environment, usually this
will be RuntimeMXBean.getInputArguments()
public boolean isDebugging()
true
if the JVM is in debug mode. This method may be used
by test classes to take additional action to disable code paths that
interfere with debugging if required.true
if the current JVM is in debug mode, false
otherwise