Service Testing

You can test Exonum services with the help of the Exonum TestKit. It allows to test transaction execution and APIs in the synchronous environment (that is, without consensus algorithm and network operation involved). Tests are executed in the same system process as the service code itself, allowing to more easily debug service business logic (for example, panics).

This document describes testing of Rust services with exonum-testkit crate. For Java instructions please refer to the documentation.


In most cases you want to install exonum-testkit as a development dependency, so it would be compiled for tests only. For this, add the dependency to the dev-dependencies section of the Cargo.toml file of your project:

exonum-testkit = "1.0.0"


The newest version of the testkit crate may differ from the one specified above. To find out the newest version, you may look at the repository page on Beware that different versions of testkit are compatible with different versions of the core library.

Simple usage

Transactions testing

The primary goal of this kind of tests is to check the business logic of your service, encapsulated in the transaction handlers.

Suppose the service interface is defined in the crate my-service as

/// Transaction payload.
pub struct MyTransaction { /* ... */ }

/// Service interface.
pub trait MyInterface<Ctx> {
    type Output;
    fn do_something(&self, context: Ctx, arg: MyTransaction) -> Self::Output;

/// Service implementation.
#[derive(ServiceFactory, ServiceDispatcher)]
pub struct MyService;
// Actual implementation skipped for brevity.

For writing your first test create tests directory according to the cargo integration testing manual. After that, create file tests/ with the content similar to the one written below.

use my_service::{MyService, MyInterface, MyTransaction, MySchema};
use exonum::crypto::gen_keypair;
use exonum_testkit::TestKitBuilder;

const SERVICE_ID: u32 = 100;

fn my_transaction_works() {
    // Create simple testkit network.
    let service_instance = MyService
        .into_default_instance(SERVICE_ID, "my-service");
    let mut testkit = TestKitBuilder::validator()
    // Create transaction.
    let payload = MyTransaction::new(/* ... */);
    // Generate a random keypair to sign the transaction.
    let keypair = gen_keypair();
    // Get a signed transaction.
    let tx = keypair.do_something(SERVICE_ID, payload);
    // Commit it into blockchain.
    let block = testkit.create_block_with_transaction(tx);
    // Check that the transaction executed successfully.
    // Check the expected result.
    let snapshot = testkit.snapshot();
    let schema = MySchema::new(&snapshot);

Make sure that you have full coverage of the business logic in the execute method of your transactions.

Testkit also allows to check different orderings of transactions, including transactions for multiple services. This could allow to more efficiently test margin cases that are quite difficult (but not impossible) to produce in the real network.

// Create transactions.
let keypair = gen_keypair();
let tx1 = keypair.do_one_thing(MyTransaction::new(/* ... */));
let tx2 = keypair.do_other_thing(OtherTransaction::new(/* ... */));
// Commit them into the blockchain.
testkit.create_block_with_transactions(vec![tx1, tx2]);
// Check the expected result.

API testing

The basic workflow for testing API endpoints of an Exonum service with the testkit is as follows:

  1. Define the MyServiceApi trait for the TestKitApi structure that covers the whole API of your service.
  2. Implement functions that use some transactions as test data to fill the storage.
  3. Create the tests that check all of your endpoints.
Workflow example
// API trait definition.
trait MyServiceApi {
    fn get_public_data(&self) -> PublicDataResponse;
    fn get_private_data(&self) -> PrivateDataResponse;
    fn post_private_data(&self, data: &PrivateData)
        -> PostPrivateDataResponse;

impl MyServiceApi for TestKitApi {
    fn get_public_data(&self) -> PublicDataResponse {

    fn get_private_data(&self) -> PrivateDataResponse {

    fn post_private_data(&self, query: &PrivateDataQuery)
        -> PostPrivateDataResponse

fn my_api_test() {
    let mut testkit = TestKitBuilder::validator()
    fill_storage_with_data(&mut testkit);
    // Check API responses
    let api = testkit.api();
        ApiResponsePublicData::new(/* ... */),

In some situations, it can be useful to see the content of requests and corresponding responses. exonum-testkit provides simple logging implementation for this purpose. You can use RUST_LOG environment variable to enable logs:

RUST_LOG=exonum_testkit=trace cargo test

Advanced Usage

The testkit allows to test more complex behaviors of Exonum services, such as getting data from external sources and reconfiguring the service.

Oracles Testing

The oracle is a service which can produce transactions with external data after commit of the block. The Bitcoin anchoring service is an example of an oracle. Just like a real Exonum node, the testkit maintains a pool of unconfirmed transactions (aka the mempool). Thus, transactions created by the oracle service during the after_commit execution will be stored in TestKit memory pool and can be verified accordingly.

// Assume that we have a service which creates transaction
// with the height of the latest committed block after commit:
pub trait HandleCommitInterface<Ctx> {
    type Output;

    fn report_commit(&self, context: Ctx, height: Height)
        -> Self::Output;

#[derive(ServiceFactory, ServiceDispatcher)]
pub struct HandleCommitService;
// `Service` implementation skipped...

// Create testkit with the service.
let mut testkit = TestKitBuilder::validator()

// Call the `after_commit` event.

// Check that `after_commit` has been invoked
// at the correct height.
let payload = Height(1);
let id = HandleCommitService::INSTANCE_ID;
let tx =, payload);


In order to invoke a after_commit hook, you need to create a block with one of the create_block* methods of the testkit.

If the oracle has to fetch any data from external world, you need to create a mock object that would generate said external data to accomplish testing.

// Provide a mock object for the service.
let mut cruel_world = ExternalApiMock::new();
let service_factory =
let mut testkit = TestKitBuilder::validator()

// Expect a request from the service.
    .expect_api_call(ApiCallInfo { /* ... */ })
    .with_ok_response(ApiResponse { /* ... */ });

// Call the `after_commit` hook.
let payload = MyOracleTx::new(/* ... */);
let expected_tx = testkit
    .report_something(MyOracleService::INSTANCE_ID, payload);

// Check that the expected transaction is in the memory pool.

Testing Lifecycle Events

If an Exonum service has its own configuration, which is updated through the supervisor, you may want to test the response to a configuration change. To do this with the testkit, you can include the supervisor service with a centralized setup and perform configuration changes by generating supervisor transactions (in this case, a transaction to update the config of the service under test).

By analogy, you may test service instantiation / resuming logic.

Testing Data Migrations

The testkit crate provides a separate framework for testing data migrations; it is placed in the migrations module of the crate. Using this framework, you can set up initial data for the service, execute one or more migration scripts and ensure that the migration outcome is as expected. (That is, data is transformed as designed, outdated indexes are removed, new indexes are added, etc.) Additionally, you can test that your migration scripts are fault-tolerant, that is, properly process aborts.