Class DisableOnDebug
- java.lang.Object
-
- org.junit.rules.DisableOnDebug
-
- All Implemented Interfaces:
TestRule
public class DisableOnDebug extends java.lang.Object implements TestRule
TheDisableOnDebug
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 asTimeout
that may be disabled during debugging may be wrapped in aDisableOnDebug
.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 } }
- Since:
- 4.12
-
-
Constructor Summary
Constructors Constructor Description DisableOnDebug(TestRule rule)
Create aDisableOnDebug
instance with the timeout specified in milliseconds.DisableOnDebug(TestRule rule, java.util.List<java.lang.String> inputArguments)
Visible for testing purposes only.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Statement
apply(Statement base, Description description)
Modifies the method-runningStatement
to implement this test-running rule.boolean
isDebugging()
Returnstrue
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
-
-
-
Field Detail
-
rule
private final TestRule rule
-
debugging
private final boolean debugging
-
-
Constructor Detail
-
DisableOnDebug
public DisableOnDebug(TestRule rule)
Create aDisableOnDebug
instance with the timeout specified in milliseconds.- Parameters:
rule
- to disable during debugging
-
DisableOnDebug
DisableOnDebug(TestRule rule, java.util.List<java.lang.String> inputArguments)
Visible for testing purposes only.- Parameters:
rule
- the rule to disable during debugginginputArguments
- arguments provided to the Java runtime
-
-
Method Detail
-
apply
public Statement apply(Statement base, Description description)
Description copied from interface:TestRule
Modifies the method-runningStatement
to implement this test-running rule.- Specified by:
apply
in interfaceTestRule
- Parameters:
base
- TheStatement
to be modifieddescription
- ADescription
of the test implemented inbase
- Returns:
- a new statement, which may be the same as
base
, a wrapper aroundbase
, or a completely new Statement. - See Also:
TestRule.apply(Statement, Description)
-
isDebugging
private static boolean isDebugging(java.util.List<java.lang.String> arguments)
Parses arguments passed to the runtime environment for debug flagsOptions specified in:
- Parameters:
arguments
- the arguments passed to the runtime environment, usually this will beRuntimeMXBean.getInputArguments()
- Returns:
- true if the current JVM was started in debug mode, false otherwise.
-
isDebugging
public boolean isDebugging()
Returnstrue
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.- Returns:
true
if the current JVM is in debug mode,false
otherwise
-
-