How to mock a responder flow in corda

旧街凉风 提交于 2019-12-11 08:48:44

问题


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.

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?


回答1:


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


来源:https://stackoverflow.com/questions/49749451/how-to-mock-a-responder-flow-in-corda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!