I found Hamcrest
convenient to use with JUnit
. Now I am going to use ScalaTest
. I know I can use Hamcrest
but I wonder if I really should. Does not ScalaTest
provide similar functionality ? Is there any other Scala library for that purpose (matchers) ?
Do people use Hamcrest
with ScalaTest
?
Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
Hamcrest vs TestNG TestNG is a testing framework, and as noted above, Hamcrest is a matcher framework. Both frameworks allow us to make assertions; it is here where Hamcrest does a better job than TestNG. TestNG is a testing framework, and as noted above, Hamcrest is a matcher framework.
The Hamcrest framework was designed to accommodate different types of unit testing frameworks. For example, Hamcrest can be used with TestNG and JUnit (all versions). The Hamcrest framework is also used with mocking frameworks such as JMock, EasyMock, and Mockito.
As Michael said, you can use ScalaTest's matchers. Just make sure you extend Matchers
in your test class. They can very well replace functionality of Hamcrest, leverage Scala features and look more natural in Scala to me.
Here, you can compare Hamcrest and ScalaTest matchers on 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 a lot more you can do with ScalaTest (e.g. use Scala pattern matching, assert what can/cannot compile, ...)
Scalatest has build-in matchers. Also we use expecty. In some cases it's more concise and flexible than matchers (but it uses macros, so it requires at least 2.10 version of Scala).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With