Line asserts
CLI Assured provides several assertion methods for verifying the content of stdout or stderr output.
All line asserts check lines among the full output in any order.
|
All assertion methods that accept |
hasLines(String…)
0.0.1
hasLines(String…) asserts that the given complete lines are present:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello\nWorld")
.then()
.stdout()
// Fail unless both lines are present (any order)
.hasLines("Hello", "World")
.execute()
.assertSuccess();
doesNotHaveLines(String…)
0.0.1
doesNotHaveLines(String…) asserts that the given lines are not present:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello")
.then()
.stdout()
// Fail if the line "Goodbye" is present
.doesNotHaveLines("Goodbye")
.execute()
.assertSuccess();
hasLinesContaining(String…)
0.0.1
hasLinesContaining(String…) asserts that at least one line contains each of the given substrings:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Fail unless some line contains "World"
.hasLinesContaining("World")
.execute()
.assertSuccess();
doesNotHaveLinesContaining(String…)
0.0.1
doesNotHaveLinesContaining(String…) asserts that no line contains any of the given substrings:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Fail if any line contains "foo" or "bar"
.doesNotHaveLinesContaining("foo", "bar")
.execute()
.assertSuccess();
hasLinesContainingCaseInsensitive(String…)
0.0.1
hasLinesContainingCaseInsensitive(String…) works like hasLinesContaining, but uses case-insensitive comparison:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Case-insensitive substring match
.hasLinesContainingCaseInsensitive("hello")
.execute()
.assertSuccess();
hasLinesMatching(String…)
0.0.1
hasLinesMatching(String…) asserts that at least one line matches each given regular expression.
The regex is evaluated using Matcher.find() rather than Matcher.matches(), so it matches a substring by default:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Fail unless some line matches the regex
.hasLinesMatching("Hello .*")
.execute()
.assertSuccess();
doesNotHaveLinesMatching(String…)
0.0.1
doesNotHaveLinesMatching(String…) asserts that no line matches the given regular expressions:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Fail if any line matches the regex
.doesNotHaveLinesMatching("Error.*")
.execute()
.assertSuccess();
hasLineCount(int)
0.0.1
Use hasLineCount(int) to assert that the output contains an exact number of lines:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Line 1\nLine 2\nLine 3")
.then()
.stdout()
// Fail unless stdout has exactly 3 lines
.hasLineCount(3)
.execute()
.assertSuccess();
For more complex conditions, use hasLineCount(LongPredicate, String) with a custom predicate.
The description string supports ${stream} and ${actual} placeholders:
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Line 1\nLine 2\nLine 3")
.then()
.stdout()
// Assert using a custom predicate
.hasLineCount(
count -> count >= 2,
"Expected at least 2 lines in ${stream} but found ${actual} lines")
.execute()
.assertSuccess();
Encoding
0.0.1
By default, CLI Assured reads the output using UTF-8 encoding.
Use charset(Charset) to change the character encoding:
import java.nio.charset.StandardCharsets;
import org.cliassured.CliAssured;
CliAssured
.command("echo", "Hello World!")
.then()
.stdout()
// Read stdout using ISO-8859-1 encoding
.charset(StandardCharsets.ISO_8859_1)
.hasLinesContaining("Hello")
.execute()
.assertSuccess();