Logging recieved message with akka testkit

653 views Asked by At

I'm trying to code some test cases for my actors, the problem is that some tests fail and I can't get a clue of why are they failing.

In a normal scenario my code is working fine (Without any tests), but for my tests I'm mocking some parts of the twitter4j library, which makes them somehow complicated.

The tests are failing and there is no more useful info about it.

Is there a way to log the received message with akka testkit and scalatest?

1

There are 1 answers

0
cmbaxter On

If you want to be able to log all received messages, there are three things you must do:

  1. Wrap your receive function on the actor you want to enable logging for with a LoggingReceive (from the akka.event package).

  2. Set the akka.actor.debug.receive setting from the config to true

  3. Make sure your logging config will show messages at the DEBUG level

So your actor might look something like this:

import akka.actor._
import akka.event.LoggingReceive

class MyActor extends Actor{
  def receive = LoggingReceive{
    case msg => ...
  }
}

If you set things up like that you should start seeing all incoming messages logged to the DEBUG level for your actor under test. You can read more on this in the Testing Actor Systems section of the Akka Docs, specifically the Tracing Actor Invocations subsection.