gpt4 book ai didi

java - Akka:未在测试 ActorSystem 中登录异常

转载 作者:行者123 更新时间:2023-11-30 08:04:03 24 4
gpt4 key购买 nike

我正在尝试使用测试套件对我的 Java Akka actor 进行单元测试

public class AggregationActor extends UntypedActor {
final private LoggingAdapter logger = Logging.getLogger(getContext().system(), this);
private final ActorRef mergeActor;
private final ActorRef saveActor;
private final AggregationHelper aggregationHelper;

这个 AggregationActor 包含一些我通过构造函数传入的依赖项

我使用 TestProbes 来模拟 ActorRefs,使用 EasyMock 来模拟 AggregationHelper

我的 AggregationActorTest 单元测试包含以下内容

@Before
public void setup() {
ActorSystem actorSystem = ActorSystem.apply();
mergeActor = new TestProbe(actorSystem);
saveActor = new TestProbe(actorSystem);
aggregationHelper = EasyMock.createMock(AggregationHelper.class);
aggregationActor = TestActorRef.apply(Props.create(AggregationActor.class, mergeActor.ref(), saveActor.ref(), aggregationHelper), actorSystem);
}

@Test
public void mySampleTest() throws Exception {
reset(aggregationHelper);
// Set expectations on the aggregationHelper
replay(blockToTicketMapHelper);
aggregationActor.tell(new AggregationVO();
saveActor.expectMsg(new SaveVO());
}

我发现,如果我的 AggregationActor.onReceive() 抛出异常,则不会记录该异常,或者我没有看到它通过堆栈抛出

我只得到:java.lang.AssertionError:断言失败:expectMsg期间超时(3秒)

如何设置我的测试 ActorSystem 以便不抑制任何异常?

最佳答案

尝试使用 docs 中所述的 TestActorRef#receive 方法。使用此方法而不是 tell 可确保任何抛出的异常不会被 TestActorRef 吞没。

例如:

@Test
public void mySampleTest() throws Exception {
reset(aggregationHelper);
// Set expectations on the aggregationHelper
replay(blockToTicketMapHelper);
aggregationActor.receive(new AggregationVO();
saveActor.expectMsg(new SaveVO());
}

关于java - Akka:未在测试 ActorSystem 中登录异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31418339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com