我正在使用Corda V2(Java(,并且有一个迫切需要单元测试的合约。 但很快就掉进了一个兔子洞,试图对net.corda.core.transactions.LedgerTransaction
的创造进行逆向工程。 是否有任何框架支持测试MyContract
方法verify()
?
public class MyContract implements Contract {
// This is used to identify our contract when building a transaction.
public static final String MY_CONTRACT_ID = "com.exchange.data.address.MyContract";
// Our Create command.
public static class Create implements CommandData {
}
@Override
public void verify(LedgerTransaction tx) {
final CommandWithParties<MyContract.Create> command = requireSingleCommand(tx.getCommands(), MyContract.Create.class);
requireThat(check -> {
// Constraints on the shape of the transaction.
check.using("No inputs should be consumed when exchanging data.", tx.getInputs().isEmpty());
check.using("There should be one output state of type MyState.", tx.getOutputs().size() == 1);
final MyState out = tx.outputsOfType(MyState.class).get(0);
final Party sender = out.getSender();
final Party receiver = out.getReceiver();
check.using("The sender and the receiver cannot be the same entity.", !sender.getName().equals(receiver.getName()));
// Constraints on the signers.
final List<PublicKey> signers = command.getSigners();
check.using("There must be two signers.", signers.size() == 2);
check.using("The sender and receiver must be signers.", signers.containsAll(
ImmutableList.of(sender.getOwningKey(), receiver.getOwningKey())));
return null;
});
}
是的,您可以使用Corda的合约测试框架。
API 文档在这里:https://docs.corda.net/api-testing.html#contract-testing(请注意,这些文档适用于 V3。V2 不存在这些文档(。
您可以在此处查看一些示例用法:https://github.com/corda/cordapp-example/blob/release-V2/java-source/src/test/java/com/example/contract/IOUContractTests.java。