Corda.v2 合同的单元测试支持?

Unit testing support for Corda.v2 Contract?

我正在使用 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