我正在尝试使用测试工具包对我的Java Akka演员进行单元测试
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
My 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: assertion failed: timeout (3 seconds) during expectMsg
如何设置我的测试ActorSystem,以便不抑制任何异常?
答案 0 :(得分:1)
尝试使用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());
}