How to perform instance validation using Scala (Test)

I am trying to incorporate ScalaTest into my Java project by replacing all JUnit ScalaTests tests. At some point I want to check if Guice Injector is entering 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 with the same as 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) } } } 

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

+61
java scala junit scalatest
Dec 19 '11 at 13:16
source share
6 answers

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

+86
Dec 19 '11 at 13:27
source share

With Scalatest 2.2.x (maybe even earlier) you can use: anInstance mustBe an[SomeClass]

+34
Aug 08 '14 at
source share

If you want to be smaller than JUnit-esque, and if you want to use ScalaTest matches, you can write your own property match that matches the type (erase the stroke type).

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

Then you can write statements such as:

 house.door should be (anInstanceOf[WoodenDoor]) 

instead

 assert(house.door instanceof WoodenDoor) 
+28
Dec 19 '11 at 15:09
source share

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

Example:

 OuterType foo = blah foo match { case subFoo : SubType => { subFoo.thingSubTypeDoes // no need to cast, use match variable } case subFoo => { // fallthrough code } } 
+12
May 01 '14 at 10:24
source share

Consolidating the Guillaume ScalaTest discussion link (and another James Moore related discussion) to two methods updated for ScalaTest 2.x and Scala 2.10 (to use ClassTag, not the 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 ) } 
+2
May 15 '14 at 16:36
source share

I use 2.11.8 to execute a statement with collections. The newer syntax is as follows:

 val scores: Map[String, Int] = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8) scores shouldBe a[Map[_, _]] 
0
Aug 21 '17 at 15:15
source share



All Articles