Skip to content

Instantly share code, notes, and snippets.

@johnlcox
Last active August 29, 2015 13:56
Show Gist options
  • Save johnlcox/9176734 to your computer and use it in GitHub Desktop.
Save johnlcox/9176734 to your computer and use it in GitHub Desktop.
class MyInjectable() {
val myString = "my String"
}
trait MyTrait {
@Inject
val myInjectable: MyInjectable = null // Initialize to null so that it isn't abstract and will be injected by guice. Is there a better way to do this though?
}
class MyObject extends Controller with MyTrait {}
@RunWith(classOf[JUnitRunner])
class MixinInjectionSpec extends Specification {
"Guice" should {
"inject the mixin trait" in {
val injector = Guice.createInjector()
val myObject = injector.getInstance(classOf[MyObject])
myObject.myInjectable.myString must equalTo("my String")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment