Kre*_*ver 6 mockito akka scalatest
我想监视我的actor实例,但它不能简单地用new关键字创建.我想出了以下解决方案:
val testActorSpy = spy(TestActorRef(new TestActor).underlyingActor)
val testActorRef = TestActorRef(testActorSpy )
Run Code Online (Sandbox Code Playgroud)
但这样我创造了一个不必要的演员.有没有更清洁的解决方案?
所以我对 Akka Actor 系统的理解是你应该通过属性来做到这一点,对吧?
因此,通过 Props 创建演员,并且在测试时只需将间谍返回给演员。
因此这应该给你一个结果:
val testActorRef = TestActorRef(spy(new TestActor))
val testActorSpy = testActorRef.underlyingActor
Run Code Online (Sandbox Code Playgroud)
请注意,当 Actor 重新启动时,底层 Actor 会被销毁。所以嘲笑这可能不是最好的选择。
如果直接使用参与者而不是通过系统,您也可以测试事物并绕过线程底层系统。
请参阅此(下面的 java 代码粘贴)。
static class MyActor extends UntypedActor {
public void onReceive(Object o) throws Exception {
if (o.equals("say42")) {
getSender().tell(42, getSelf());
} else if (o instanceof Exception) {
throw (Exception) o;
}
}
public boolean testMe() { return true; }
}
@Test
public void demonstrateTestActorRef() {
final Props props = Props.create(MyActor.class);
final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "testA");
final MyActor actor = ref.underlyingActor();
assertTrue(actor.testMe());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
971 次 |
| 最近记录: |