如何在corda中模拟响应程序流

如何在corda中模拟响应程序流,corda,Corda,我试图在我的单元测试中模拟一个响应程序流,我的响应程序流进行了几次验证,处理配置和账外服务。我希望模拟该值以始终返回true,这样单元测试就不会依赖于网络中的其他组件 其目的仅用于单元测试,是否有任何方法可以使用API模拟响应,因为我知道我们必须在模拟网络设置期间注册响应程序类?只需定义一个虚拟响应程序流,并在设置模拟网络时注册它而不是真实的响应程序流: public class FlowTests { private MockNetwork network; private S

我试图在我的单元测试中模拟一个响应程序流,我的响应程序流进行了几次验证,处理配置和账外服务。我希望模拟该值以始终返回
true
,这样单元测试就不会依赖于网络中的其他组件


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

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

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);
    }
}
公共类流测试{
专用网络;
私有启动的mocknode a;
私有启动的mocknode b;
@InitiatedBy(ExampleFlow.Initiator.class)
公共静态类DummyResponder扩展了FlowLogic{
非公开最终流程会议其他方会议;
公共Dummy响应程序(FlowSession otherPartySession){
this.otherPartySession=otherPartySession;
}
@暂停
@凌驾
public Void call()引发流异常{
otherPartySession.send(true);
返回null;
}
}
@以前
公共作废设置(){
网络=新的MockNetwork(ImmutableList.of(“com.example.contract”);
a=network.createPartyNode(空);
b=网络.createPartyNode(空);
//对于实际节点,这会自动发生,但我们必须手动注册测试流。
for(StartedMockNode节点:不可变列表,共(a,b)){
registerInitiatedFlow(DummyResponder.class);
}
runNetwork();
}
@之后
公共无效拆卸(){
network.stopNodes();
}
@统治
public final ExpectedException exception=ExpectedException.none();
@试验
public void flowUsesDummyResponder()引发ExecutionException、InterruptedException{
ExampleFlow.Initiator flow=新的ExampleFlow.Initiator(-1,b.getInfo().getLegalIdentities().get(0));
CordaFuture future=a.startFlow(流量);
runNetwork();
布尔布尔布尔=future.get();
assertEquals(真,布尔);
}
}