Use the ProvideSecurityManager
rule to provide a specific security manager for your test. The system's
security manager is restored after the test.
System Rules needs at least JUnit 4.9.
All examples below are using the @Rule annotation,
but the rules can be used with the @ClassRule
The ClearSystemProperties rule deletes the property before the test
and restores the original value of the property when the test is
The ProvideSystemProperty rule provides an arbitrary value for a
system property to a test. After the test the original value is
You could also use a single instance of the rule to achieve the
You can use a properties file to supply properties for the
ProvideSystemProperty rule. The file can be from the file system or the
class path. In the first case use
and in the second case use
The RestoreSystemProperties rule undoes changes of all system properties when the test finishes (whether it passes or fails).
System.err and System.out
The SystemErrRule and the SystemOutRule
help you to create tests for classes that write to
System.err or System.out. They can record
everything written to System.err or System.out.
The text is available by calling getLog().
If you verify logs that contain line separators than the separators
are different (e.g. Linux: \n, Windows: \r\n).
Use getLogWithNormalizedLineSeparator() for a log that
always has the line separator \n.
If your code under test writes raw binary data to System.err
or System.out then you can read it by means of
The log can be cleared if you want to discard some text that has been
written to the log.
The output is still written to System.err and
System.out. In general this is not necessary. Avoiding
the output may speed up the test and reduce the clutter on the
commandline. You can disable the output by calling mute().
Muting and logging can be combined.
In case of a failed test it is sometimes helpful to see the output.
This is when the method muteForSuccessfulTests() comes
The TextFromStandardInputStream rule helps you
to create tests for classes which read from
System.in. You specify the lines provided by
System.in, by calling
The example's class under test reads two numbers from
System.in and calculates the sum of these numbers.
Class Under Test
If you want to test that your class under test handles exception correctly then you can tell TextFromStandardInputStream to throw an exception after all characters have been read.
Disallow write to System.out and System.err
The rules DisallowWriteToSystemErr and
DisallowWriteToSystemOut let tests fail if they try to write to
System.err or System.out. The rules are a nice
replacement for static code analysis because they cover external libraries,
If your code calls System.exit(), then your test
stops and doesn't finish. The ExpectedSystemExit
rule allows in-test specification of expected
System.exit() calls. Furthermore you cannot use
JUnit's assert methods because of the abnormal termination of
your code. As a substitute you can provide an
Assertion object to the
Some care must be taken if your system under test creates a
new thread and this thread calls System.exit(). In
this case you have to ensure that the test does not finish
before System.exit() is called.
Class Under Test
If you need to set an environment variable then use the
EnvironmentVariables rule. It does all the tedious work for
setting environment variables and reverts your changes after the test.
If your test needs some environment variables not to be present then you
can clear it.
If you need a special security manager to test your code, you may
provide it by using the ProvideSecurityManager rule. This rule
replaces the system's security manager with yours throughout the
Marc Philipp (email@example.com)
Stefan Birkner (firstname.lastname@example.org)