Awesome AssertJ: Check String Starts Or Ends With A Given Value
Writing assertions using the nice fluent API of AssertJ is a joy. Besides some of the basic assertions like isEqualTo
AssertJ also has specific assertions for specific types. For example if we want write an assertion to check if a String
value starts or ends with an expected value we can use the startsWith(String)
or endsWith(String)
methods. If we don’t care that a character is upper or lower case we can also use startsWithIgnoringCase(String)
or endsWithIgnoringCase(String)
. Each of the methods also has a counterpart method to check the String
value doesn’t start or end with an expected value. For example we can use doesNotStartWith(String)
to assert a value does not start with the expected value.
Instead of using the specific startsWith…(String)
or endsWith…(String)
methods we can also use the more generic matches
method. Now we need to specify a Pattern
or Predicate
that matches our requirement. This give some more flexbility but is also more verbose. To test there is not a match we can use the doesNotMatch
method.
In the following example we write assertions to check if a String
value starts with an expected value:
package mrhaki;
import org.junit.jupiter.api.Test;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
class StringStartsWith {
@Test
void testStringStartsWith() {
assertThat("Awesome AssertJ").startsWith("Awesome");
// We can also check ignoring the casing of the String value.
assertThat("Awesome AssertJ").startsWithIgnoringCase("aweSome");
}
@Test
void testStringDoesNotStartWith() {
// Or we check a String value doesn't start with an expected value.
assertThat("Amazing Assertj").doesNotStartWith("Awesome");
// We can also check ignoring the casing of the String value.
assertThat("Amazing Assertj").doesNotStartWithIgnoringCase("aweSome");
}
@Test
void testStringStartsWithRegex() {
// We can use regular expressions as well, but it is more verbose.
assertThat("Awesome AssertJ").matches(Pattern.compile("^Awesome.*"));
// Also here we can ignore casing.
assertThat("Awesome AssertJ").matches(Pattern.compile("^awesome.*", Pattern.CASE_INSENSITIVE));
// And check String value doesn't start with expected value.
assertThat("Amazing AssertJ").doesNotMatch(Pattern.compile("^Awesome.*"));
}
@Test
void testStringStartsWithPredicate() {
// We can use a Predicate as well, but it is more verbose.
assertThat("Awesome AssertJ").matches(s -> s.startsWith("Awesome"));
// We are more flexible as we can use for example specify an offset.
assertThat("Awesome AssertJ").matches(s -> s.startsWith("some", 3));
// If we want to ignore casing we have to first convert our value.
assertThat("Awesome AssertJ").matches(s -> s.toLowerCase().startsWith("awesome"));
// And check String value doesn't start with expected value.
assertThat("Amazing AssertJ").matches(s -> !s.startsWith("Awesome"));
}
}
And in the next example we write assertions to check if a String
value ends with an expected value:
package mrhaki;
import org.junit.jupiter.api.Test;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
class StringEndsWith {
@Test
void testStringEndsWith() {
assertThat("Awesome AssertJ").endsWith("AssertJ");
// We can also check ignoring the casing of the String value.
assertThat("Awesome AssertJ").endsWithIgnoringCase("assertJ");
}
@Test
void testStringDoesNotEndWith() {
// Or we we check our String values doesn't end with an expected value.
assertThat("Awesome Asciidoc").doesNotEndWith("AssertJ");
// We can also check ignoring the casing of the String value.
assertThat("Awesome Asciidoc").doesNotEndWithIgnoringCase("assertJ");
}
@Test
void testStringEndsWithRegex() {
// We can use regular expressions as well, but it is more verbose.
assertThat("Awesome AssertJ").matches(Pattern.compile(".*AssertJ$"));
// Also here we can ignore casing.
assertThat("Awesome AssertJ").matches(Pattern.compile(".*AssertJ$", Pattern.CASE_INSENSITIVE));
// And check String value doesn't end with expected value.
assertThat("Amazing Asciidoc").doesNotMatch(Pattern.compile(".*AssertJ$"));
}
@Test
void testStringEndsWithPredicate() {
// We can use a Predicate as well, but it is more verbose.
assertThat("Awesome AssertJ").matches(s -> s.endsWith("AssertJ"));
// If we want to ignore casing we have to first convert our value.
assertThat("Awesome AssertJ").matches(s -> s.toLowerCase().endsWith("assertj"));
// And check String value doesn't end with expected value.
assertThat("Awesome Asciidoc").matches(s -> !s.endsWith("AssertJ"));
}
}
Written with AssertJ 3.24.2.