Tags: stefanbirkner/system-lambda
Tags
Replace assertThrownBy with assertThrows assertThrows is the correct method name of JUnit Jupiter's assertion for exceptions. All examples use JUnit Jupiter for assertions.
Add methods for tapping err and out simultaneously
The output of command-line applications is usually the output to the
standard output stream and the error output stream intermixed. The new
methods allow capturing the intermixed output.
@test
void application_writes_text_to_System_err_and_out(
) throws Exception {
String text = tapSystemErrAndOut(() -> {
System.err.print("text from err");
System.out.print("text from out");
});
assertEquals("text from errtext from out", text);
}
@test
void application_writes_mutliple_lines_to_System_err_and_out(
) throws Exception {
String text = tapSystemErrAndOutNormalized(() -> {
System.err.println("text from err");
System.out.println("text from out");
});
assertEquals("text from err\ntext from out\n", text);
}
Fix examples for tapping System.err and System.out println and '\n' chars have not been used correctly.
Support Callable for "withEnvironmentVariable"
This allows to write more precise and readable tests. The assertion can
be outside of the "withEnvironmentVariable" statement and "execute" can
be called with a one-line Lambda expression. E.g.
@test
void execute_code_with_environment_variables(
) throws Exception {
String value = withEnvironmentVariable("key", "the value")
.execute(() -> System.getenv("key"));
assertEquals("the value", value);
}