A method annotated @BeforeClass
will run exactly once before the test methods in that class run.
A method annotated with @AfterClass
will run exactly once after all the tests in the class have been run.
Redirect System.err
before the tests begin so the output is not cluttered with expected error messages:
// This class tests a lot of error conditions, which
// Xalan annoyingly logs to System.err.
private PrintStream systemErr;
@BeforeClass protected void redirectStderr() {
systemErr = System.err; // Hold on to the original value
System.setErr(new PrintStream(new ByteArrayOutputStream()));
}
Restore it after the tests end:
@AfterClass protected void tearDown() {
// restore the original value
System.setErr(systemErr);
}
Use judiciously; has the potential to violate the independence of the tests, introduce unexpected coupling, and make tests order dependent