user11661115
user11661115

Reputation:

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

   }

Upvotes: 2

Views: 271

Answers (1)

Mario Galic
Mario Galic

Reputation: 48410

Try

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

Upvotes: 1

Related Questions