The following works if you use the Before trait, instead of calling the Injector directly in the body of the context trait:
package examples
import org.specs2.mutable._
import org.specs2.specification.Scope
import com.google.inject.{Inject, Module, Guice, Binder}
class TestSpec extends Specification {
"this is an example" in new context {
emailService must not be(null)
}
trait context extends GuiceContext {
def module = new MyModule
@Inject var emailService : EmailService = null
}
}
class EmailService
class MyModule extends Module {
def configure(binder: Binder) {
binder.bind(classOf[EmailService]).toInstance(new EmailService);
}
}
trait GuiceContext extends Before {
def module: Module
def before = Guice.createInjector(module).injectMembers(this)
}
Eric.