Package | Description |
---|---|
org.junit.rules | |
org.junit.runners |
Provides standard
Runner implementations. |
Modifier and Type | Class and Description |
---|---|
class |
DisableOnDebug
The
DisableOnDebug Rule allows you to label certain rules to be
disabled when debugging. |
class |
ErrorCollector
The ErrorCollector rule allows execution of a test to continue after the
first problem is found (for example, to collect _all_ the incorrect rows in a
table, and report them all at once):
public static class UsesErrorCollectorTwice {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test
public void example() {
collector.addError(new Throwable("first thing went wrong"));
collector.addError(new Throwable("second thing went wrong"));
collector.checkThat(getResult(), not(containsString("ERROR!
|
class |
ExpectedException
The
ExpectedException rule allows you to verify that your code
throws a specific exception. |
class |
ExternalResource
A base class for Rules (like TemporaryFolder) that set up an external
resource before a test (a file, socket, server, database connection, etc.),
and guarantee to tear it down afterward:
public static class UsesExternalResource {
Server myServer= new Server();
@Rule
public ExternalResource resource= new ExternalResource() {
@Override
protected void before() throws Throwable {
myServer.connect();
};
@Override
protected void after() {
myServer.disconnect();
};
};
@Test
public void testFoo() {
new Client().run(myServer);
}
}
|
class |
RuleChain
The RuleChain rule allows ordering of TestRules.
|
class |
Stopwatch
The Stopwatch Rule notifies one of its own protected methods of the time spent by a test.
|
class |
TemporaryFolder
The TemporaryFolder Rule allows creation of files and folders that should
be deleted when the test method finishes (whether it passes or
fails).
|
class |
TestName
The TestName Rule makes the current test name available inside test methods:
public class TestNameTest {
@Rule
public TestName name= new TestName();
@Test
public void testA() {
assertEquals("testA", name.getMethodName());
}
@Test
public void testB() {
assertEquals("testB", name.getMethodName());
}
}
|
class |
TestWatcher
TestWatcher is a base class for Rules that take note of the testing
action, without modifying it.
|
class |
Timeout
The Timeout Rule applies the same timeout to all test methods in a class:
public static class HasGlobalLongTimeout {
@Rule
public Timeout globalTimeout= new Timeout(20);
@Test
public void run1() throws InterruptedException {
Thread.sleep(100);
}
@Test
public void infiniteLoop() {
while (true) {}
}
}
Each test is run in a new thread.
|
class |
Verifier
Verifier is a base class for Rules like ErrorCollector, which can turn
otherwise passing test methods into failing tests if a verification check is
failed
public static class ErrorLogVerifier {
private ErrorLog errorLog = new ErrorLog();
@Rule
public Verifier verifier = new Verifier() {
@Override public void verify() {
assertTrue(errorLog.isEmpty());
}
}
@Test public void testThatMightWriteErrorLog() {
// ...
}
}
|
Modifier and Type | Method and Description |
---|---|
RuleChain |
RuleChain.around(TestRule enclosedRule)
Create a new
RuleChain , which encloses the nextRule with
the rules of the current RuleChain . |
static RuleChain |
RuleChain.outerRule(TestRule outerRule)
Returns a
RuleChain with a single TestRule . |
Constructor and Description |
---|
DisableOnDebug(TestRule rule)
Create a
DisableOnDebug instance with the timeout specified in
milliseconds. |
Constructor and Description |
---|
RunRules(Statement base,
Iterable<TestRule> rules,
Description description) |
Modifier and Type | Method and Description |
---|---|
protected List<TestRule> |
ParentRunner.classRules() |
protected List<TestRule> |
BlockJUnit4ClassRunner.getTestRules(Object target) |
Copyright © 2002–2018 JUnit. All rights reserved.