Building and Verifying Transactions

Testing your contracts requires transactions. Building a test transaction is similar to a real-world CorDapp. The following sections contain example chains of function calls:

For more information, see the full Transaction Builder API documentation.

In Java, you must explicitly get the UTXO Ledger Service and create a transaction builder:

UtxoSignedTransaction issueTransaction = getLedgerService()
                .createTransactionBuilder()
                .addCommand(new SampleCommand.Issue())
                .addOutputState(new SampleState(List.of(aliceKey), 10, aliceKey))
                .setNotary(notaryName)
                .setTimeWindowUntil(Instant.now().plus(Duration.ofSeconds(60)))
                .addSignatories(aliceKey)
                .toSignedTransaction();

In Kotlin, the transaction building process becomes simpler using the Kotlin DSL:

val issueTransaction = buildTransaction {
    addCommand(new SampleCommand.Issue())
    addOutputState(new SampleState(List.of(aliceKey), 10, aliceKey))
    setNotary(notaryName)
    setTimeWindowUntil(Instant.now().plus(Duration.ofSeconds(60)))
    addSignatories(aliceKey)
}

Once you have built your transaction, you can verify it by calling one of the following assertion functions:

  • assertVerifies — use if you are expecting your test to pass the contract validation.
  • assertFailsWith — use if you are expecting your test to fail contract validation with an exception message exactly matching the provided string.
  • assertFailsWithMessageContaining — use if you are expecting your test to fail contract validation with an error message containing the provided string.

Was this page helpful?

Thanks for your feedback!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.

We're sorry this page wasn't helpful. Let us know how we can make it better!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Create an issue

Create a new GitHub issue in this repository - submit technical feedback, draw attention to a potential documentation bug, or share ideas for improvement and general feedback.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.