繁体   English   中英

测试Akka actor时检查日志消息是否缺失

[英]Check for lack of log messages when testing akka actors

使用TestKit测试Akka actor时, https: TestKit显示了如何验证是否记录了给定消息。

有没有办法检查是否缺少消息?

我已经设置了参与者,以便在收到未处理的消息时调用一种方法来记录类似“收到了意外消息”的日志。 在我的测试中,我想验证该消息是否从未记录过,即使该测试似乎成功了也是如此。 有没有办法做到这一点?

我正在使用Akka 2.5和Java 10。

这取决于您的实现。 您可以执行以下两项操作之一:

1)创建一个TestKit探针,并使其订阅系统的eventStream

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

然后最后检查一下,以您的情况为0,探针收到了多少条消息。请使用许多“期望...”方法之一。

2)文档告诉您如何检查日志消息,因此只需断言获得“收到意外消息”的次数为0。

同样,根据演员的实施情况,上述操作可能无效。

祝好运!

为了提供一些细节,这是我需要做的:

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

src/test/resources/application.conf

akka.loggers = [akka.testkit.TestEventListener ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM