Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking logger in Play! 2 Framework
    primarykey
    data
    text
    <p>I have a Play! 2 application (Scala) and there I have some classes that need to perform logging when they get errors.</p> <p>I would like to be able to unit test that these logging actions actually take place in the right conditions. To do so, I need to be able to mock the logger, but I am getting some issues with Mockito. What I have (simplified) looks like</p> <pre><code>import play.api.{ Logger, LoggerLike } trait BaseService { val log: LoggerLike def fail(reason: String) { log.error(reason) } } object Service extends BaseService { val log = Logger } </code></pre> <p>and then in the tests</p> <pre><code>import org.specs2.mutable._ import org.specs2.mock._ import services.BaseService object Service extends BaseService with Mockito { val log = mock[play.api.Logger] def verify(key: String) = { there was one(log).error(key) } } class ServiceSpec extends Specification { "failures should be logged" in { Service.fail("foo") Service.verify("foo") } } </code></pre> <p>But I get the error</p> <pre><code>[error] NullPointerException: null (Logger.scala:43) [error] play.api.LoggerLike$class.isErrorEnabled(Logger.scala:43) [error] play.api.Logger.isErrorEnabled(Logger.scala:147) [error] play.api.LoggerLike$class.error(Logger.scala:127) [error] play.api.Logger.error(Logger.scala:147) [error] services.BaseService$class.fail(Service.scala:19) [error] Service$.fail(ServiceSpec.scala:11) ... </code></pre> <p>I tried adding</p> <pre><code>log.isErrorEnabled returns true log.error(any[String]) returns {} </code></pre> <p>but then even the initialization of Service fails.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload