Run a command and get the output
Although CLI Assured was primarily designed for testing CLI applications, it also offers shortcuts to run a command and get the output as a stream of lines or as a byte array without too much typing.
Redirect stderr to stdout and get all output lines
0.1.0
CommandSpec.lines() is a convenience shorthand that executes the command, redirects stderr to stdout, captures all output,
asserts success, and returns a stream of lines captured on stdout.
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
// lines() executes the command, redirects stderr to stdout,
// captures all output, asserts success,
// and returns the output as stream of lines
Stream<String> lines = CliAssured
.command("echo", "Line 1\nLine 2")
.lines();
Assertions.assertThat(lines).containsExactly("Line 1", "Line 2");
Redirect stderr to stdout and get all output as a byte array
0.1.0
CommandSpec.bytes() is a convenience shorthand that executes the command, redirects stderr to stdout, captures all output,
asserts success, and returns the raw bytes captured on stdout.
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
// bytes() executes the command, redirects stderr to stdout,
// captures all output, asserts success, and returns raw bytes
byte[] bytes = CliAssured
.command("cat")
.stdin(stdin -> stdin.write(new byte[] {(byte) 0xc0, (byte) 0xfe, (byte) 0xba, (byte) 0xbe}))
.bytes();
Assertions.assertThat(bytes).asHexString().isEqualTo("C0FEBABE");
|
The above snippets are a shot version of the following:
|