I am trying to test a class
@Singleton
class Foo @Inject()(bar: Bar)(implicit ec: ExecutionContext) {
def doSomething = bar.doSomethingInBar
}
class Bar {
def doSomethingInBar = true
}
through a Specification
class that is mentioned below
class FooTest @Inject()(foo: Foo) extends Specification {
"foo" should {
"bar" in {
foo.doSomething mustEqual (true)
}
}
}
Now when I run this I get the following error
Can't find a constructor for class Foo
I followed the solution mentioned here
And defined an Injector
object Inject {
lazy val injector = Guice.createInjector()
def apply[T <: AnyRef](implicit m: ClassTag[T]): T =
injector.getInstance(m.runtimeClass).asInstanceOf[T]
}
and a lazy val foo: Foo = Inject[Foo]
inside my Specification class. It solves my constructor initialization problem but I am getting this error now.
[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
答案 0 :(得分:0)
您需要提供隐式ExecutionEnv
才能使代码正常工作
@RunWith(classOf[JUnitRunner])
class FooTest(implicit ee: ExecutionEnv) extends Specification {
"foo" should {
"bar" in {
foo.doSomething mustEqual (true)
}
}
}
现在代码中的某个地方,你需要初始化foo构造,你可以通过构造函数传递它 - 这是依赖注入的重点。