Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:16:26+00:00 2026-06-10T12:16:26+00:00

I have a Play! 2 application (Scala) and there I have some classes that

  • 0

I have a Play! 2 application (Scala) and there I have some classes that need to perform logging when they get errors.

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

import play.api.{ Logger, LoggerLike }

trait BaseService {
  val log: LoggerLike

  def fail(reason: String) {
    log.error(reason)
   }
}

object Service extends BaseService {
  val log = Logger
}

and then in the tests

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")
  }
}

But I get the error

[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)
...

I tried adding

log.isErrorEnabled returns true
log.error(any[String]) returns {}

but then even the initialization of Service fails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:16:27+00:00Added an answer on June 10, 2026 at 12:16 pm

    I’m not much of a Mockito expert, but I think your mock is malfunctioning here since the stack trace shows the actual play Logger methods being called. This is likely because you’re trying to mock a Scala object here. If instead you mock the trait like

     val log = mock[play.api.LoggerLike]
    

    you’ll get past this specific error and on to the next problem – your verification doesn’t work:

    [error]   The mock was not called as expected: 
    [error] Argument(s) are different! Wanted:
    [error] loggerLike.error(
    [error]     ($anonfun$apply$mcV$sp$1) <function0>
    [error] );
    [error] -> at Service$$anonfun$verify$1.apply$mcV$sp(ServiceSpec.scala:10)
    [error] Actual invocation has different arguments:
    [error] loggerLike.error(
    [error]     ($anonfun$fail$1) <function0>
    [error] );
    [error] -> at services.BaseService$class.fail(x.scala:57)
    [error]  (ServiceSpec.scala:17)
    

    This is caused by the fact that log messages are passed as by-name arguments (and thus anonymous functions) instead of strings, see this question for further discussion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a play application and have some models. One of them contains
in my application i have the playvideo page where video will play, below that
i have to make an application where i need to play two video simultaniously,on
I have recently launched my application into the Google Play store but unfortunately some
I have a Play Framework application that sends emails using SMTP server. Now I
Lets say I have an already functioning Play 2.0 framework based application in Scala
Using play framework 1.2.4 with scala. I have few play jobs that looks like
I have created iphone application to play some video files. I kept all the
I have 20 different flash files that i need to play randomly when a
I have play-framework application that uses one module which I've created by play new-module

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.