0
votes

When testing an Akka actor with the TestKit, https://doc.akka.io/docs/akka/2.5/testing.html shows how to verify that a given message was logged.

Is there a way to check for the lack of a message?

I have my actors set up to call a method the logs something like "Unexpected message received" when an unhandled message is received. In my test, I would like to verify that that message is never logged, even if the test otherwise seems to succeed. Is there a way to do that?

I am using Akka 2.5 and Java 10.

2

2 Answers

2
votes

It depends on your implementation. You could do one of two things:

1) Create a TestKit probe and make it subscribe to your system's eventStream

yourActorSystemInTheTest.eventStream().subscribe(yourProbe.getRef(), UnhandledMessage.class);

And then at the end check to see how many messages the probe received, in your case 0. Use one of the many "expect..." methods at your disposal.

2) The docs tell you how to check for log messages, so just assert that the number of times you get the "Unexpected message received" is 0.

Again, depending on your actors' implementation, the above might not work.

Good Luck!

1
votes

To provide some details, here is what I needed to do:

import akka.event.Logging;
import akka.testkit.javadsl.EventFilter;
import akka.testkit.javadsl.TestKit;
...

@Test
public void noUnhandledTest() {

  new TestKit(system) {{
    new EventFilter(Logging.Warning.class, system).
       occurrences(0).
       matches("unhandled event").
       intercept(() -> {
         try {
           <actual test code>

           // Intercept needs a supplier
           return 0;
         } catch (Exception e) {
           // Suppliers can't throw
           throw new RuntimeException(e);
         }
       });
  }};
}

In src/test/resources/application.conf:

akka.loggers = [akka.testkit.TestEventListener ]