This article is a rewrite of an older one done in Java. This one is done in Kotlin instead.
Intro
Last time, I went over what a Hamcrest Matcher was, how it’s used, and how to make one. In this article, I will explain more advanced steps in the creation of Hamcrest Matchers. First, I’ll share how to make your matchers more easily type-safe, then some techniques for stateless Matchers, then finally how to cut down on so many static imports on your test classes. I’ll also give some quick tips on naming your static factory methods.
Typesafe Matchers
You may have noticed in the matches() method that we developed last time, I put in a type check. Potentially, you’ll need null checks too, because the method accepts an Any?
, which allows nulls. The type check should seem strange, since we inherited from a class that has a generic type that we specified to be a String
. Continue Reading