[英]Check for lack of log messages when testing akka actors
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. 使用
TestKit
测试Akka actor时, https: TestKit
显示了如何验证是否记录了给定消息。
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. 我正在使用Akka 2.5和Java 10。
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 1)创建一个TestKit探针,并使其订阅系统的eventStream
yourActorSystemInTheTest.eventStream().subscribe(yourProbe.getRef(), UnhandledMessage.class); 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. 然后最后检查一下,以您的情况为0,探针收到了多少条消息。请使用许多“期望...”方法之一。
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. 2)文档告诉您如何检查日志消息,因此只需断言获得“收到意外消息”的次数为0。
Again, depending on your actors' implementation, the above might not work. 同样,根据演员的实施情况,上述操作可能无效。
Good Luck! 祝好运!
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
: 在
src/test/resources/application.conf
:
akka.loggers = [akka.testkit.TestEventListener ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.