如何在Corda中模拟响应者流程 [英] How to mock a responder flow in corda

查看:28
本文介绍了如何在Corda中模拟响应者流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单元测试中模拟响应者流,我的响应者流进行了一些验证,这些验证涉及配置和关闭分类账服务.我想模拟该值以始终返回 true ,以便单元测试对网络中的其他组件没有任何依赖关系.

I am trying to mock a responder flow inside my unit tests, my responder flow does several validations that deals with configurations and off ledger services. I would like to mock the value to always return a true so that the unit test does not have any dependencies on the other components in the network.

此目的仅用于单元测试,有什么方法可以使用API​​模拟响应,因为我知道我们必须在模拟网络设置过程中注册响应者类?

The purpose is only for unit tests, is there any way I could mock the response using API as I am aware that we have to register the responder classes during the mock network setup?

推荐答案

只需定义一个虚拟响应程序流,并在设置模拟网络时注册该响应程序流,而不是实际的响应程序流:

Simply define a dummy responder flow, and register that instead of the real responder flow when setting up the mock network:

public class FlowTests {
    private MockNetwork network;
    private StartedMockNode a;
    private StartedMockNode b;

    @InitiatedBy(ExampleFlow.Initiator.class)
    public static class DummyResponder extends FlowLogic<Void> {

        private final FlowSession otherPartySession;

        public DummyResponder(FlowSession otherPartySession) {
            this.otherPartySession = otherPartySession;
        }

        @Suspendable
        @Override
        public Void call() throws FlowException {
            otherPartySession.send(true);
            return null;
        }
    }

    @Before
    public void setup() {
        network = new MockNetwork(ImmutableList.of("com.example.contract"));
        a = network.createPartyNode(null);
        b = network.createPartyNode(null);
        // For real nodes this happens automatically, but we have to manually register the flow for tests.
        for (StartedMockNode node : ImmutableList.of(a, b)) {
            node.registerInitiatedFlow(DummyResponder.class);
        }
        network.runNetwork();
    }

    @After
    public void tearDown() {
        network.stopNodes();
    }

    @Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void flowUsesDummyResponder() throws ExecutionException, InterruptedException {
        ExampleFlow.Initiator flow = new ExampleFlow.Initiator(-1, b.getInfo().getLegalIdentities().get(0));
        CordaFuture<Boolean> future = a.startFlow(flow);
        network.runNetwork();
        Boolean bool = future.get();
        assertEquals(true, bool);
    }
}

这篇关于如何在Corda中模拟响应者流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆