AssertJ
If you need full AssertJ power over the captured output, use captureAll() to store all lines in memory,
then access them via CommandResult.stdout().lines() or CommandResult.stdout().bytes() after execution:
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
import org.cliassured.CommandResult;
// Capture all lines and use AssertJ for assertions
CommandResult result = CliAssured
.command("echo", "Hello\nWorld")
.then()
.stdout()
// Capture all stdout lines in memory
.captureAll()
.execute()
.assertSuccess();
// Access the captured lines and assert with AssertJ
Stream<String> lines = result.stdout().lines();
Assertions.assertThat(lines)
.containsExactly("Hello", "World");
You can also assert on the raw bytes:
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
import org.cliassured.CommandResult;
CommandResult result = CliAssured
.command("echo", "Hello")
.then()
.stdout()
// Capture all stdout output in memory
.captureAll()
.execute()
.assertSuccess();
// Access the raw bytes
byte[] bytes = result.stdout().bytes();
Assertions.assertThat(new String(bytes)).contains("Hello");
|
Using |
Using AssertJ on individual lines
0.0.1
Use lines(Consumer<String>) to run AssertJ assertions on each line as it is produced.
The consumer is called once per line from an internal I/O thread:
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Use lines() to assert each individual line with AssertJ
.lines(line -> Assertions.assertThat(line).doesNotContain("ERROR"))
.execute()
.assertSuccess();