我正在尝试将 ScalaTest 合并到我的 Java 项目中;用 ScalaTests 替换所有 JUnit 测试。在某一时刻,我想检查 Guice 的 Injector 是否注入了正确的类型。在Java中,我有一个这样的测试:
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);
}
}
但是我对 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)
}
}
}
它抱怨该值instanceof
不是Door
//的成员Window
。Roof
我不能instanceof
在 Scala 中使用这种方式吗?