Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Mokito to mock out an Akka Actor's log object

I've tried a couple of things which seem to compile but throw NullPointer exceptions during unit testing so I'm wondering how I could potentially overcome the limitations in unit-testing. I have a class which looks like this:

class LogWriter extends Actor{
  def receive{
    case x:Timing => log.info(x toString)
    case x:Event => log.info(x toString)
    case x:Any => log.warning("Improper message sent to LogWriter, %s".format(x toString))
  }
}

But attempting to unit test using Specs2 and the Mockito support with something along the lines of:

class LogWriterSpec extends Mokito with Specification{
  val mockedLog = mock[Logger]

  class MockedLogWriter extends LogWriter{
    @transient override val log = mockedLog
  }

  val writer = actorOf(new MockedLogWriter).start

  def testTiming = {
    val message = Timing("testTiming", 15 seconds)
    writer !! (message, 400)
    there was one(mockedLog).info(message toString)
  }

  def is = "A LogWriter" ^
    "should write a Timing message to log.info" ! testTiming ^
  end
}

while compiling results in the aforementioned NullPointerException:

[akka:event-driven:dispatcher:global-10] ERROR akka.actor.Actor$ - Problem
java.lang.NullPointerException
    at akka.util.Logger.warning_$qmark(Logging.scala:43)
    at akka.util.Logger.warning(Logging.scala:117)

I tried changing it to use some mixin trait which overrode the "log" object of the Akka Logging trait but the compiler didn't allow it. The compiler reply was something along the lines of "we don't want you making an inadvertent mistake." Ugh! I want that "mistake."

Anyone know of another way? I'm not stuck to Mockito and welcome any suggestions.

like image 500
wheaties Avatar asked Oct 10 '22 11:10

wheaties


1 Answers

Scalamock is a native Scala mock framework (among others, it allows mocking functions). You can find a complete example here:

http://www.paulbutcher.com/2011/11/scalamock-step-by-step/

Perhaps it will solve your problem.

like image 126
paradigmatic Avatar answered Nov 09 '22 20:11

paradigmatic