我正试着测试一堂课
@Singleton
class Foo @Inject()(bar: Bar)(implicit ec: ExecutionContext) {
def doSomething = bar.doSomethingInBar
}
class Bar {
def doSomethingInBar = true
}通过下面提到的Specification类
class FooTest @Inject()(foo: Foo) extends Specification {
"foo" should {
"bar" in {
foo.doSomething mustEqual (true)
}
}
}现在,当我运行这个程序时,我会得到以下错误
Can't find a constructor for class Foo我跟着the solution mentioned here
并定义了一个Injector
object Inject {
lazy val injector = Guice.createInjector()
def apply[T <: AnyRef](implicit m: ClassTag[T]): T =
injector.getInstance(m.runtimeClass).asInstanceOf[T]
}和一个lazy val foo: Foo = Inject[Foo]在我的规范类中。它解决了我的构造函数初始化问题,但我现在得到了这个错误。
[error] ! check the calculate assets function
[error] Guice configuration errors:
[error]
[error] 1) No implementation for scala.concurrent.ExecutionContext was bound.
[error] while locating scala.concurrent.ExecutionContext发布于 2016-12-14 05:27:41
您需要提供一个隐式ExecutionEnv来使代码工作。
@RunWith(classOf[JUnitRunner])
class FooTest(implicit ee: ExecutionEnv) extends Specification {
"foo" should {
"bar" in {
foo.doSomething mustEqual (true)
}
}
}现在,在代码中的某个地方,您需要初始化foo构造,并且可以通过构造函数传递它--这就是有依赖注入的意义所在。
https://stackoverflow.com/questions/41061241
复制相似问题