119

I'm trying to incorporate ScalaTest into my Java project; replacing all JUnit tests with ScalaTests. At one point, I want to check if Guice's Injector injects the correct type. In Java, I have a test like this:

public class InjectorBehaviour {
    @Test
    public void shouldInjectCorrectTypes() {
        Injector injector = Guice.createInjector(new ModuleImpl());
        House house = injector.getInstance(House.class);

        assertTrue(house.door() instanceof WoodenDoor);
        assertTrue(house.window() instanceof BambooWindow);
        assertTrue(house.roof() instanceof SlateRoof);
    }
}

But I have a problem doing the same with ScalaTest:

class InjectorSpec extends Spec {
    describe("An injector") {
        it("should inject the correct types") {
            val injector = Guice.createInjector(new ModuleImpl)
            val house = injector.getInstance(classOf[House])

            assert(house.door instanceof WoodenDoor)
            assert(house.window instanceof BambooWindow)
            assert(house.roof instanceof SlateRoof)
        }
    }
}

It complains that the value instanceof is not a member of Door/Window/Roof. Can't I use instanceof that way in Scala?

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
helpermethod
  • 59,493
  • 71
  • 188
  • 276

6 Answers6

128

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

anInstance mustBe a[SomeClass]
martin-g
  • 17,243
  • 2
  • 23
  • 35
124

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.

agilesteel
  • 16,775
  • 6
  • 44
  • 55
30

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)
EdChum
  • 376,765
  • 198
  • 813
  • 562
Guillaume Belrose
  • 2,478
  • 4
  • 23
  • 24
  • +1 That looks very nice, and even understandable for non-programming people (assuming they know what an instance is :-)). – helpermethod Dec 19 '11 at 15:15
  • If syntax sugar is what you are after, with some refactoring you might be able to write house.door should be (madeOf[Wood]) or house.door should be (madeOf[Bamboo]). – Guillaume Belrose Dec 19 '11 at 15:34
  • 1
    Also see https://groups.google.com/d/topic/scalatest-users/HeOKgs5PC2o/discussion – James Moore Dec 30 '12 at 18:30
18

The current answers about isInstanceOf[Type] and junit advice are good but I want to add one thing (for people who got to this page in a non-junit-related capacity). In many cases scala pattern matching will suit your needs. I would recommend it in those cases because it gives you the typecasting for free and leaves less room for error.

Example:

OuterType foo = blah
foo match {
  case subFoo : SubType => {
    subFoo.thingSubTypeDoes // no need to cast, use match variable
  }
  case subFoo => {
    // fallthrough code
  }
}
alexbobp
  • 181
  • 1
  • 3
  • 1
    The recommended way to test a pattern match in ScalaTest is to use `inside(foo)` instead of `foo match). See http://www.scalatest.org/user_guide/using_matchers#matchingAPattern – Rich Dougherty Aug 24 '17 at 19:37
3

Consolidating Guillaume's ScalaTest discussion reference (and another discussion linked to by James Moore) into two methods, updated for ScalaTest 2.x and Scala 2.10 (to use ClassTag rather than manifest):

import org.scalatest.matchers._
import scala.reflect._

def ofType[T:ClassTag] = BeMatcher { obj: Any =>
  val cls = classTag[T].runtimeClass
  MatchResult(
    obj.getClass == cls,
    obj.toString + " was not an instance of " + cls.toString,
    obj.toString + " was an instance of " + cls.toString
  )
}

def anInstanceOf[T:ClassTag] = BeMatcher { obj: Any =>
  val cls = classTag[T].runtimeClass
  MatchResult(
    cls.isAssignableFrom(obj.getClass),
    obj.getClass.toString + " was not assignable from " + cls.toString,
    obj.getClass.toString + " was assignable from " + cls.toString
  )
}
Raman
  • 17,606
  • 5
  • 95
  • 112
2

I use 2.11.8 to do the assertion with collections. The newer syntax is as follows:

val scores: Map[String, Int] = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)
scores shouldBe a[Map[_, _]] 
aristotll
  • 8,694
  • 6
  • 33
  • 53
  • 3
    Due to erasure you cannot check the `Map`'s type parameters. What you wrote is the same as writing `scores shouldBe a[Map[_, _]]`. This is mentioned here: http://www.scalatest.org/user_guide/using_matchers#checkingAnObjectsClass – Rich Dougherty Aug 24 '17 at 19:34