我正在尝试在单元测试中模拟响应器流,我的响应器流做了几个处理配置和关闭分类帐服务的验证。我想模拟值以始终返回true
,以便单元测试与网络中的其他组件没有任何依赖关系。
目的仅限于单元测试,有没有什么方法可以使用API模拟响应,因为我知道我们必须在模拟网络设置期间注册响应者类?
答案 0 :(得分: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);
}
}