Output redirects

Redirect std[out|err] to a file

0.0.1

Use redirect(Path) to write the output of stdout or stderr to a file. CLI Assured opens the file, writes all output to it and closes it when the process terminates.

import java.nio.file.Path;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;

Path outFile = Path.of("target/out.txt");

CliAssured
    .command("echo", "Hello World!")
    .then()
        .stdout()
            // Redirect stdout to a file
            .redirect(outFile)
    .execute()
    .assertSuccess();

// Verify the file contains the output
Assertions.assertThat(outFile).content().contains("Hello World!");

Redirect std[out|err] to an OutputStream

0.0.1

You can also redirect to an arbitrary OutputStream using redirect(OutputStream). Note that CLI Assured will not close the stream — the caller is responsible for closing it. This is useful for appending output from multiple commands to a single stream.

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

CliAssured
    .command("echo", "Hello World!")
    .then()
        .stdout()
            // Redirect stdout to an OutputStream
            .redirect(baos)
    .execute()
    .assertSuccess();

// CLI Assured does not close the stream; the caller is responsible
Assertions.assertThat(baos.toString(StandardCharsets.UTF_8)).contains("Hello World!");