How to do an instanceof check with Scala(Test)

Scala is not Java. Scala just does not have the operator instanceof instead it has a parametric method called isInstanceOf[Type].

You might also enjoy watching a ScalaTest Crash Course.


With Scalatest 2.2.x (maybe even earlier) you can use:

anInstance mustBe a[SomeClass]

If you want to be less JUnit-esque and if you want to use ScalaTest's matchers, you can write your own property matcher that matches for type (bar type erasure).

I found this thread to be quite useful: http://groups.google.com/group/scalatest-users/browse_thread/thread/52b75133a5c70786/1440504527566dea?#1440504527566dea

You can then write assertions like:

house.door should be (anInstanceOf[WoodenDoor])

instead of

assert(house.door instanceof WoodenDoor)