corda / net.corda.testing.node / MockNetwork


open class MockNetwork

A mock node brings up a suite of in-memory services in a fast manner suitable for unit testing. Components that do IO are either swapped out for mocks, or pointed to a Jimfs in memory filesystem or an in memory H2 database instance.

Java users can use the constructor that takes an (optional) MockNetworkParameters builder, which may be more convenient than specifying all the defaults by hand. Please see MockNetworkParameters for the documentation of each parameter.

Mock network nodes require manual pumping by default: they will not run asynchronous. This means that for message exchanges to take place (and associated handlers to run), you must call the runNetwork method. If you want messages to flow automatically, use automatic pumping with a thread per node but watch out for code running parallel to your unit tests: you will need to use futures correctly to ensure race-free results.

By default a single notary node is automatically started, which forms part of the network parameters for all the nodes. This node is available by calling defaultNotaryNode.



MockNetwork(cordappPackages: List<String>, parameters: MockNetworkParameters = MockNetworkParameters())
MockNetwork(parameters: MockNetworkParameters)MockNetwork(cordappPackages: List<String>, defaultParameters: MockNetworkParameters = MockNetworkParameters(), networkSendManuallyPumped: Boolean = defaultParameters.networkSendManuallyPumped, threadPerNode: Boolean = defaultParameters.threadPerNode, servicePeerAllocationStrategy: ServicePeerAllocationStrategy = defaultParameters.servicePeerAllocationStrategy, notarySpecs: List<MockNetworkNotarySpec> = defaultParameters.notarySpecs, networkParameters: NetworkParameters = defaultParameters.networkParameters)

A mock node brings up a suite of in-memory services in a fast manner suitable for unit testing. Components that do IO are either swapped out for mocks, or pointed to a Jimfs in memory filesystem or an in memory H2 database instance.



val cordappPackages: List<String>

A List of cordapp packages to scan for any cordapp code, e.g. contract verification code, flows and services.


val defaultNotaryIdentity: Party

Return the identity of the default notary node.


val defaultNotaryNode: StartedMockNode

Returns the single notary node on the network. Throws an exception if there are none or more than one.


val defaultParameters: MockNetworkParameters

The default parameters for the network. If any of the remaining constructor parameters are specified then their values are taken instead of the corresponding value in defaultParameters.


val networkParameters: NetworkParameters

The network parameters to be used by all the nodes. NetworkParameters.notaries must be empty as notaries are defined by notarySpecs.


val networkSendManuallyPumped: Boolean

If false then messages will not be routed from sender to receiver until you use the MockNetwork.runNetwork method. This is useful for writing single-threaded unit test code that can examine the state of the mock network before and after a message is sent, without races and without the receiving node immediately sending a response. The default is false, so you must call runNetwork.


val nextNodeId: Int

In a mock network, nodes have an incrementing integer ID. Real networks do not have this. Returns the next ID that will be used.


val notaryNodes: List<StartedMockNode>

Returns the list of notary nodes started by the network.


val notarySpecs: List<MockNetworkNotarySpec>

The notaries to use in the mock network. By default you get one mock notary and that is usually sufficient.


val servicePeerAllocationStrategy: ServicePeerAllocationStrategy

How messages are load balanced in the case where a single compound identity is used by multiple nodes. You rarely if ever need to change that, it's primarily of interest to people testing notary code.


val threadPerNode: Boolean

If true then each node will be run in its own thread. This can result in race conditions in your code if not carefully written, but is more realistic and may help if you have flows in your app that do long blocking operations. The default is false.



fun baseDirectory(nodeId: Int): Path

Get the base directory for the given node id.


fun createNode(parameters: MockNodeParameters): StartedMockNode
fun createNode(legalName: CordaX500Name? = null, forcedID: Int? = null, entropyRoot: BigInteger = BigInteger.valueOf(random63BitValue()), configOverrides: MockNodeConfigOverrides? = null): StartedMockNode

Create a started node with the given parameters.


fun createPartyNode(legalName: CordaX500Name? = null): StartedMockNode

Create a started node with the given identity.


fun createUnstartedNode(parameters: MockNodeParameters = MockNodeParameters()): UnstartedMockNode
fun createUnstartedNode(legalName: CordaX500Name? = null, forcedID: Int? = null, entropyRoot: BigInteger = BigInteger.valueOf(random63BitValue()), configOverrides: MockNodeConfigOverrides? = null): UnstartedMockNode

Create an unstarted node with the given parameters.


fun runNetwork(rounds: Int = -1): Unit

Asks every node in order to process any queued up inbound messages. This may in turn result in nodes sending more messages to each other, thus, a typical usage is to call runNetwork with the rounds parameter set to -1 (the default) which simply runs as many rounds as necessary to result in network stability (no nodes sent any messages in the last round).


fun startNodes(): Unit

Start all nodes that aren't already started.


fun stopNodes(): Unit

Stop all nodes.


fun waitQuiescent(): Unit

Block until all scheduled activity, active flows and network activity has ceased.

Extension Functions


fun Any.contextLogger(): <ERROR CLASS>

When called from a companion object, returns the logger for the enclosing class.