Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hamcrest and ScalaTest

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?

like image 314
Michael Avatar asked Jul 14 '13 04:07

Michael


People also ask

What is Hamcrest in testing?

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.

What is Hamcrest used for?

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.

Why we use TestNG instead of Hamcrest?

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.

Can we use Hamcrest with TestNG?

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.


2 Answers

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, ...)

like image 151
Mifeet Avatar answered Oct 22 '22 03:10

Mifeet


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).

like image 20
Sergey Passichenko Avatar answered Oct 22 '22 04:10

Sergey Passichenko