Mock class with Configuration as arguments

446 views Asked by At

How do I mock the class which has Configuration(play.api) injected as arguments to its constructor?

class SomeScalaClass @Inject(config: Configuration){
    val someValue = config.get[String]("someValueInConfig")
    def abc:Int = {
     ..
     ..
    }
}

I am using scalamock. I am trying to test a class which has "SomeScalaClass" injected as dependency to it:

class ClassToTest @Inject()(obj: SomeScalaClass){.....}

I am getting error when I do this in my test Fixture:

val someScalaClassMock = mock[SomeScalaClass]
val classToTestObj = new ClassToTest(someScalaClassMock)

Error is: not found: value someScalaClassMock

I am new to scala so not sure how to tackle this issue. Thanks in Advance

1

There are 1 answers

0
Manoj Kumar On

You can try making someValue val as lazy val or extracting the config value inside the method definition if required inside a single method.

Or you can use typesafe config which does not need any Injection.

import com.typesafe.config.ConfigFactory


val someValue= ConfigFactory.load().getString("someValueInConfig")