Skip to content

Instantly share code, notes, and snippets.

@rethab
Last active January 23, 2016 06:01
Show Gist options
  • Save rethab/01fde763d10f29273d43 to your computer and use it in GitHub Desktop.
Save rethab/01fde763d10f29273d43 to your computer and use it in GitHub Desktop.
Enhanced WithApplication class to test Play 2.4 applications with dependency injection using
/**
* Provides Messages implicit and the default Guice-Container
* into the context for testing Play 2.4 applications.
*
* Sample:
*
* "use the overridden bindigs" in new WithFancyApp(
* lang = Lang("en", "US"),
* overrideModules = Seq(bind[MyInterface].to[MyImplementation])
* ) {
* // test stuff with all regular bindings plus the ones from above
* }
*
*/
class WithFancyApp(lang: Lang = Lang.defaultLang,
overrideModules: Seq[GuiceableModule] = Seq()) extends
WithApplication(
app =
new GuiceApplicationBuilder()
.in(Environment(new File("."), getClass.getClassLoader, Mode.Test))
.loadConfig(env => Configuration.load(env))
.overrides(overrideModules:_*)
.bindings()
.build
) {
implicit def messages: Messages = Messages(lang, app.injector.instanceOf[MessagesApi])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment