我正在尝试为我的一个演员编写单元测试,该演员使用 TestKit 从 AbstractPersistentActorWithAtLeastOnceDelivery 派生。我需要使用 TestActorRef.create(...) 创建一个演员,因为我需要获取一个基础演员才能将模拟注入演员的实现。
我的(简化的)演员
public class MyActor extends AbstractPersistentActorWithAtLeastOnceDelivery {
@Override
public Receive createReceive() {
return receiveBuilder().match(String.class, message -> {
persist(new MessageSent(message), event -> updateState(event));
}).match(ConfirmMessage.class, confirm -> {
persist(new MessageConfirmed(confirm.deliveryId), event ->
updateState(event));
}).matchAny(message -> log.info("Received unexpected message of class {}.
Content: {}", message.getClass().getName(), message.toString())).build();
}
void updateState(Object received) {
if (received instanceof MessageSent) {
final MessageSent messageSent = (MessageSent) received;
ActorRef destinationActor =
findDestinationActor(messageSent.messageData);
deliver(actorSystem.actorSelection(destinationActor.path()),
deliveryId -> new Message(deliveryId, messageSent.messageData));
} else if (received instanceof MessageConfirmed) {
final MessageConfirmed messageConfirmed = (MessageConfirmed) received;
confirmDelivery(messageConfirmed.deliveryId);
}
}
单元测试:
@Test
public void actorTest() {
ActorSystem system = ActorSystem.create();
TestKit probe = new TestKit(system);
TestActorRef<myActor> testActor = TestActorRef.create(system, props,
probe.getRef());
MyActor myActor = testActor.underlyingActor();
injectMocks(myActor); // my method
testActor.tell("testMessage", probe.getRef());
List<Object> receivedMessages = probe.receiveN(1, FiniteDuration.create(3,
TimeUnit.SECONDS));
}
在调试器中,我看到 updateState() 中的 deliver() 方法被调用,但单元测试失败并出现错误:
断言失败:超时(3 秒),同时期待 1 条消息(得到 0)
我想知道是否可以使用 TestKit 来测试通过 TestActorRef 创建的演员,以及我的演员扩展 AbstractPersistentActorWithAtLeastOnceDelivery 的事实是否与测试失败有关