这里是Java 8和Akka 2.12:2.5.16。我正在尝试编写我的第一个(有史以来)利用Akka TestKit的Akka单元测试,并且正在努力应用我在网上找到的(非常少的)示例中看到的原则。
我的演员:
public class Child extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Init.class, init -> {
int workUnit = workService.doSomeWork();
log.info("Performed work on {}", workUnit);
}).build();
}
}
public class Master extends AbstractActor {
@Inject @Named("CHILD")
private ActorRef child;
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Init.class, init -> {
child.tell(init, self());
}).build();
}
}非常非常简单。现在,我只想编写一个单元测试,以验证当Master参与者接收到Init消息时,它是否将该消息转发给它的Child参与者。到目前为止,我最大的努力是:
@RunWith(MockitoJUnitRunner.class)
public class MasterTest {
private ActorSystem actorSystem;
@Before
public void setup() {
actorSystem = ActorSystem.create("test-system");
}
@After
public void teardown() {
Duration duration = Duration.create(10L, TimeUnit.SECONDS);
TestKit.shutdownActorSystem(actorSystem, duration, true);
actorSystem = null;
}
@Test
public void onInit_shouldSendFordwardToChild() {
// Given
TestKit testKit = new TestKit(actorSystem);
ActorRef master = actorSystem.actorOf(Props.create(Master.class, testKit));
// When
master.tell(new Init(), ActorRef.noSender());
// Then
testKit.expectMsg(Init.class);
}
}当我运行这个时,我得到:
java.lang.IllegalArgumentException: no matching constructor found on class com.me.myapp.Master for arguments [class akka.testkit.TestKit]请有人帮助我将TestKit实例连接到我的Master参与者中,并帮助我了解如何重构MasterTest,以便验证我正在尝试完成的任务。提前感谢!
发布于 2018-10-18 18:55:53
我想出来了,真不敢相信要让这件事成功是多么困难。
在application.conf中
MyAkkApp {
akka {
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 2553
}
}
}
}然后:
@RunWith(MockitoJUnitRunner.class)
public class MasterTest extends TestKit {
static ActorSystem actorSystem = ActorSystem.create("MyAkkaApp",
ConfigFactory.load().getConfig("MyAkkaApp"));
static TestProbe child; // The mock child
static ActorRef master;
@BeforeClass
public static void setup() {
child = new TestProbe(actorSystem, "Child");
master = actorSystem.actorOf(Props.create(new Creator<Actor>() {
@Override
public Actor create() throws Exception {
return new Master(child.ref());
}
}));
}
public MasterTest() {
super(actorSystem);
}
@Test
public void onInit_shouldSendFordwardToChild() {
// Given
Init init = new Init();
// When
master.tell(init, super.testActor());
// Then
child.expectMsg(init); // Child should have received it
expectNoMessage(); // Master should not be returning to sender
}
}来吧,阿克卡,伙计们!支持产量采用,采用导致标准化,标准化意味着你可以销售6位数的企业支持许可证。
https://stackoverflow.com/questions/52841110
复制相似问题