Matchers
Matchers test for some property, indicated by the name of the matcher, beyond simple equality. For example, a matcher may check whether a string is empty or whether an integer is positive. In the getting started guide, we used the assertion shouldBe
to check for equality. In fact, the assertion shouldBe
also accepts a matcher that provides for more complicated assertions.
The idea behind the shouldBe
naming convention is to lead to readable assertions, such as thisString shouldBe empty()
. To further this goal, there is an equivalent of shouldBe
, named should
; with this, matchers such as thisString
should startWith("foo")
could be read as natural language.
Many matchers are provided by KotlinTest out of the box, and each one checks for some specific property or condition. In the rest of this section, we will cover some of the most fundamental matchers.
String matchers
One of the most common set of matchers is undoubtedly the String matchers. This is not surprising, given...