As Michael said, you can use ScalaTest matchers . Just make sure you extend Matchers in the test class. They can very well replace Hamcrest functionality, use Scala functions and look more natural in Scala for me.
Here you can compare the combinations of Hamcrest and ScalaTest with a few examples:
val x = "abc" val y = 3 val list = new util.ArrayList(asList("x", "y", "z")) val map = Map("k" -> "v") // equality assertThat(x, is("abc")) // Hamcrest x shouldBe "abc" // ScalaTest // nullity assertThat(x, is(notNullValue())) x should not be null // string matching assertThat(x, startsWith("a")) x should startWith("a") x should fullyMatch regex "^a..$" // regex, no native support in Hamcrest AFAIK // type check assertThat("a", is(instanceOf[String](classOf[String]))) x shouldBe a [String] // collection size assertThat(list, hasSize(3)) list should have size 3 // collection contents assertThat(list, contains("x", "y", "z")) list should contain theSameElementsInOrderAs Seq("x", "y", "z") // map contents map should contain("k" -> "v") // no native support in Hamcrest // combining matchers assertThat(y, both(greaterThan(1)).and(not(lessThan(3)))) y should (be > (1) and not be <(3))
... and much more you can do with ScalaTest (for example, use Scala pattern matching, say that it can / cannot compile, ...)
source share