A TestRule is an alteration in how a test method, or set of test methods,
is run and reported. A
may add additional checks that cause
a test that would otherwise fail to pass, or it may perform necessary setup or
cleanup for tests, or it may observe test execution to report it elsewhere.
s can do everything that could be done previously with
methods annotated with
, but they are more powerful, and more easily
between projects and classes.
The default JUnit test runners for suites and
individual test cases recognize
s introduced in two different
s. See Javadoc for those annotations
for more information.
s can be applied to a test or suite execution. The
that executes the method or suite is passed to each annotated
in turn, and each may return a substitute or modified
, which is passed to the next
, if any. For
examples of how this can be useful, see these provided TestRules,
or write your own: