问题描述
是否可以模拟CordaRPCops,以便在项目中执行流而无需创建独立节点或内存中节点(例如在模拟网络中)?请让我知道。
I am also adding a link which I found informative regarding this from github issues QA
解决方法
CordaRPCops
中没有可用的特定类模拟TestDSL
。如果您指的是在cordapp测试中模拟节点的某些功能,则应使用MockNode。
如果要在客户端应用程序中模拟CordaRPCops
,则可以使用嘲讽来做到这一点,例如以下示例:
测试:
@Test
public void testGetStateList(){
CordaRPCOps cordaRPCOps = Mockito.mock(CordaRPCOps.class);
Service service = new Service(cordaRPCOps);
Vault.Page<MyState> myStatePage =
new Vault.Page<>(Collections.EMPTY_LIST,Collections.EMPTY_LIST,0L,Vault.StateStatus.ALL,Collections.EMPTY_LIST);
Mockito.when(cordaRPCOps.vaultQuery(MyState.class)).thenReturn(myStatePage);
service.getStateList();
}
服务:
public class Service {
CordaRPCOps cordaRPCOps;
public Service(CordaRPCOps cordaRPCOps) {
this.cordaRPCOps = cordaRPCOps;
}
public List<StateAndRef<MyState>> getStateList() {
return cordaRPCOps.vaultQuery(MyState.class).getStates();
}
}