Command result

CommandResult is returned by execute() or awaitTermination() and provides access to the outcome of a command execution.

assertSuccess()

0.0.1

assertSuccess() verifies that no exceptions occurred during execution and that all assertions defined via then() are satisfied. It throws an AssertionError if any check fails.

import java.time.Duration;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
import org.cliassured.CommandResult;

// assertSuccess() checks that no exceptions occurred
// and all assertions passed
CliAssured
    .command("echo", "Hello")
    .then()
        .stdout()
            .hasLines("Hello")
    .execute()
    .assertSuccess();

assertTimeout()

0.0.1

assertTimeout() verifies that the command was killed because it exceeded the timeout specified via execute(Duration) or execute(long). It throws an AssertionError if the process terminated normally.

import java.time.Duration;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
import org.cliassured.CommandResult;

CliAssured
    .command("sleep", "60")
    .then()
        .stdout()
            .isEmpty()
    // Wait at most 100 ms
    .execute(Duration.ofMillis(100))
    // Assert that the process was killed due to timeout
    .assertTimeout();

exitCode()

0.0.1

exitCode() returns the exit code of the terminated process.

import java.time.Duration;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
import org.cliassured.CommandResult;

CommandResult result = CliAssured
    .command("true")
    .execute();

// Access the exit code of the terminated process
int exitCode = result.exitCode();
Assertions.assertThat(exitCode).isZero();

duration()

0.0.1

duration() returns the wall-clock duration of the command execution as a java.time.Duration.

import java.time.Duration;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.cliassured.CliAssured;
import org.cliassured.CommandResult;

CommandResult result = CliAssured
    .command("sleep", "0.01")
    .execute()
    .assertSuccess();

// Access the wall-clock duration of the command execution
Duration duration = result.duration();
Assertions.assertThat(duration).isPositive();

Access the whole content of std[out|err]

0.1.0

Use CommandResult.stdout() or CommandResult.stderr() to obtain a StreamResult object, which provides access to the captured output lines, raw bytes, line count and byte count.

std[out|err] as stream of lines

0.1.0

Use stdout().lines() to get the captured output as a Stream<String>. This requires captureAll() to have been called on the stream before execution.

import java.time.Duration;
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\nWorld")
    .then()
        .stdout()
            // captureAll() is required for lines() and bytes()
            .captureAll()
    .execute()
    .assertSuccess();

// Access captured stdout lines as a Stream
Stream<String> lines = result.stdout().lines();
Assertions.assertThat(lines).containsExactly("Hello", "World");

std[out|err] as byte array

0.1.0

Use stdout().bytes() to get the captured output as a raw byte array. This also requires captureAll() to have been called on the stream before execution.

import java.time.Duration;
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()
            // captureAll() is required for lines() and bytes()
            .captureAll()
    .execute()
    .assertSuccess();

// Access captured stdout output as raw bytes
byte[] bytes = result.stdout().bytes();
Assertions.assertThat(new String(bytes)).contains("Hello");

Line count and byte count of std[out|err]

0.1.0

stdout().lineCount() and stdout().byteCount() are always available without captureAll().

import java.time.Duration;
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\nWorld")
    .then()
        .stdout()
            .hasLinesContaining("Hello")
    .execute()
    .assertSuccess();

// lineCount() and byteCount() are always available
// without captureAll()
int lineCount = result.stdout().lineCount();
long byteCount = result.stdout().byteCount();

Assertions.assertThat(lineCount).isEqualTo(2);
Assertions.assertThat(byteCount).isGreaterThan(0);