Exit code asserts

By default, CLI Assured asserts that the exit code is 0. You can customize this with the following methods on ExpectationsSpec.

exitCodeIs(int)

0.0.1

Use exitCodeIs(int) to assert that the process exits with an exact exit code:

import org.cliassured.CliAssured;

CliAssured
    // The true command exits with code 0
    .command("true")
    .then()
        // Fail unless exit code is exactly 0
        .exitCodeIs(0)
    .execute()
    .assertSuccess();

exitCodeIsAnyOf(int…​)

0.0.1

Use exitCodeIsAnyOf(int…​) when the command may exit with different codes depending on the platform or runtime conditions:

import org.cliassured.CliAssured;

CliAssured
    // The false command exits with code 1
    .command("false")
    .then()
        // Fail unless exit code is 0 or 1
        .exitCodeIsAnyOf(0, 1)
    .execute()
    .assertSuccess();

exitCodeSatisfies(IntPredicate, String)

0.0.1

Use exitCodeSatisfies(IntPredicate, String) for more complex conditions. The description string supports ${actual} as a placeholder for the actual exit code:

import org.cliassured.CliAssured;

CliAssured
    // The false command exits with code 1
    .command("false")
    .then()
        // Assert exit code with a custom predicate
        .exitCodeSatisfies(
            code -> code >= 0,
            "Expected non-negative exit code but got ${actual}")
    .execute()
    .assertSuccess();