Testing injected controllers in Play for Scala

572 views Asked by At

I'm trying to follow this example in Play's documentation to test a Controller with an injected object. I replicated the example, but I'm getting an error when trying to invoke the web page:

No implementation for test.Component was bound.

The error seems right as I'm not invoking the binding method, but how to fix this?

This is my code:

package test

import play.api.mvc._
import javax.inject.Inject
import play.api.{ Environment, Configuration }
import play.api.inject.Module

trait Component {
  def hello: String
}

class DefaultComponent extends Component {
  def hello = "default"
}

class MockComponent extends Component {
  def hello = "mock"
}


class ComponentModule extends Module {
  def bindings(env: Environment, conf: Configuration) = Seq(
    bind[Component].to[DefaultComponent]
  )
}

class Application @Inject() (component: Component) extends Controller {
  def index() = Action {
    Ok(component.hello)
  }
}
1

There are 1 answers

0
ps0604 On BEST ANSWER

add to application.conf

play {
    modules {
        enabled += test.ComponentModule 
    }
}