I tried a couple of things that seem to compile but throw NullPointer exceptions during unit testing, so I'm wondering how I can potentially overcome the limitations of unit testing. I have a class that 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 the unit test attempt to use Specs2 and Mockito support with something according to the lines:
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
}
when compiling the results in the above 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 to change it to use some mixin attribute, which overloaded the "log" object of the Akka object Logging, but the compiler did not allow it. The compiler’s response was something like “we don’t want you to make an unintentional error.” Ugh! I want this "mistake".
- -? Mockito .