アクターでいくつかのメソッドをモックする(実際のオブジェクト/アクターのメソッド実装をモックされたものに置き換える) ことによって、Akka アクターの機能をテストする方法について知りたいと思っています。
私は使用しakka.testkit.TestActorRef
ます;
また、使用しようとしましたが、使用SpyingProducer
方法が明確ではありません。(私のように、その実装内でアクターを作成した場合、それは今と同じになります)。それについてのグーグル検索結果はあまり冗長ではありません。
と を使用powemockito
しjava
ます。しかし、それは問題ではありません。どんなフレームワークを持つどんな言語でも知りたいですhow to do it in principle
(したがって、power/mockitoがどのように機能するかわからない場合は、コードを提供してください..(お願いします)、または知っているツールでそれを行う方法についての完全なアイデア。)
では、テストするアクタがあるとします。
package example.formock;
import akka.actor.UntypedActor;
public class ToBeTestedActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
getSender().tell( getHelloMessage((String) message), getSelf());
}
}
String getHelloMessage(String initMessage) { // this was created for test purposes (for testing mocking/spy capabilities). Look at the test
return "Hello, " + initMessage;
}
}
そして、このテストでは、代わりに別のものをgetHelloMessage()
返したいと考えています。
これは私の試みです:
package example.formock;
import akka.testkit.TestActorRef;
...
@RunWith(PowerMockRunner.class)
@PrepareForTest(ToBeTestedActor.class)
public class ToBeTestedActorTest {
static final Timeout timeout = new Timeout(Duration.create(5, "seconds"));
@Test
public void getHelloMessage() {
final ActorSystem system = ActorSystem.create("system");
// given
final TestActorRef<ToBeTestedActor> actorRef = TestActorRef.create(
system,
Props.create(ToBeTestedActor.class),
"toBeTestedActor");
// First try:
ToBeTestedActor actorSpy = PowerMockito.spy(actorRef.underlyingActor());
// change functionality
PowerMockito.when(actorSpy.getHelloMessage (anyString())).thenReturn("nothing"); // <- expecting result
try {
// when
Future<Object> future = Patterns.ask(actorRef, "Bob", timeout);
// then
assertTrue(future.isCompleted());
// when
String resultMessage = (String) Await.result(future, Duration.Zero());
// then
assertEquals("nothing", resultMessage); // FAIL HERE
} catch (Exception e) {
fail("ops");
}
}
}
結果:
org.junit.ComparisonFailure:
Expected :nothing
Actual :Hello, Bob