@FunctionalInterface public interface TestRule
TestRule are the only way (outside of the @Parameters concept) to
provides test fixtures, action before and after tests, etc. Only one
TestRule chain is allowed for a test class. If a test class
extends another one, one chain is allowed for each test class and they will
be chained from the upper class to the lower one.
Conceptually, a test rule, at execution time, will receive a test statement as parameter and compute another test statement.
TestRule. Once you have the outer test rule, it is possible to
chain them by using the around(TestRule) or
around(Supplier) methods. One single TestRule (which can be used as
a start of the chain, or later), can be builded by :
TestContextRule rule.before method that will produce a rule to be
executed before a test ; The after method will do the same, but
with a code to be executed after a test.
import org.mockito.Mock;
import org.mockito.Mockito;
import ch.powerunit.Rule;
import ch.powerunit.Test;
import ch.powerunit.TestRule;
import ch.powerunit.TestSuite;
public class MockitoRuleTest implements TestSuite {
public interface Mockable {
void run();
}
@Mock
private Mockable mock;
@Rule
public final TestRule testRule = mockitoRule()
.around(before(this::prepare));
public void prepare() {
Mockito.doThrow(new RuntimeException("test")).when(mock).run();
}
@Test
public void testException() {
assertWhen((p) -> mock.run()).throwException(
isA(RuntimeException.class));
}
}
In this case, the defined chain, will first apply the rule provided by
mockitoRule, and then apply the rule included inside the
around. The before method usage inside the
around will ensure the method prepare is executed
before each test.
The sequence of execution, will be :
prepare.testException.| Modifier and Type | Method and Description |
|---|---|
static TestRule |
after(Runnable after)
Build a after testrule.
|
default TestRule |
around(java.util.function.Supplier<TestRule> inner)
Add an inner rule.
|
default TestRule |
around(TestRule inner)
Add an inner rule.
|
static TestRule |
before(Runnable before)
Build a before testrule.
|
Statement<TestContext<Object>,Throwable> |
computeStatement(Statement<TestContext<Object>,Throwable> inner)
The default implementation of test rule.
|
static TestRule |
mockitoRule()
Create a rule to support mockito.
|
Statement<TestContext<Object>,Throwable> computeStatement(Statement<TestContext<Object>,Throwable> inner)
The goal of this method is to compute another test statement that will be the one to be runned.
inner - the inner statementstatic TestRule before(Runnable before)
The passed piece of code (which can for instance an inline definition or a reference to a method) that must be executed before the test itself.
before - the code to be used beforestatic TestRule after(Runnable after)
The passed piece of code (which can for instance an inline definition or a reference to a method) that must be executed after the test itself.
after - the code to be used afterdefault TestRule around(TestRule inner)
inner - the inner ruledefault TestRule around(java.util.function.Supplier<TestRule> inner)
This inner rule is created just before usage, thanks to the
Supplier object. This can be used for the case when one rule
depend on the outcome of a previous one.
inner - the supplier of the inner rulestatic TestRule mockitoRule()
Copyright © 2014. All rights reserved.