Search code examples
scalaplayframeworkconfigurationscalatest

How to pass Configuration object to controller in unit test


I have a controller as follows:

class MyController @Inject()
(
  cc : ControllerComponents,
) extends AbstractController(cc) with I18Support(

  def controllerMethod() = Action{
   ... //some impl.
  }
)

I am testing my controller in my Scalatest as follows:

"My Controller" when {

  "a user hits this controller method" should {

  val controller = new MyController( cc = stubMessageControllerComponents )

  "be a 200 OK" in {
    whenReady(controller.mycontrollerMethod().apply(FakeRequest("GET", "/"))) {
    // some test

   }

My issue is that now I have changed the controller class to inject a configuration object as follows

class MyController @Inject()
(
  config : Configuration,
  cc : ControllerComponents,
) extends AbstractController(cc) with I18Support(

  def controllerMethod() = Action{
   ... //some impl.
  }
)

I now get a compile error in my test because I am not passing in a Configuration object. How can I do that?

"My Controller" when {

  "a user hits this controller method" should {

  val controller = new MyController(
    // <- how can I pass a configuration object here 
    cc = stubMessageControllerComponents 
  )

  "be a 200 OK" in {
    whenReady(controller.mycontrollerMethod().apply(FakeRequest("GET", "/"))) {
    // some test

   }

Solution

  • Try

    new MyController(
      config = Configuration(ConfigFactory.load()) 
      cc = stubMessageControllerComponents 
    )