【问题标题】:How to mock a responder flow in corda如何在corda中模拟响应者流
【发布时间】:2018-04-24 21:29:27
【问题描述】:

我试图在我的单元测试中模拟一个响应程序流,我的响应程序流进行了几个处理配置和账本外服务的验证。我想模拟该值以始终返回true,以便单元测试对网络中的其他组件没有任何依赖关系。

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

【问题讨论】:

    标签: corda


    【解决方案1】:

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

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 2020-07-09
      • 1970-01-01
      • 2015-05-03
      • 2021-08-27
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多