Benjamin
Benjamin

Reputation: 115

How to Unit test Service & controller (kotlin) in a Cordapp?

I have gone through many documentations for getting a sample of unit testing service and controller in a Cordapp, not the flows (that is already done using in corda docs). Can anyone please help me to get an example cordapp which implemented service unit testing?

Upvotes: 1

Views: 337

Answers (2)

Benjamin
Benjamin

Reputation: 115

We can use Mockito module for mocking and stabbing that is required for unit testing service functions and APIs.

This link will direct more on how to mock CordaRPCops using mockito as an example.

Upvotes: 0

davidawad
davidawad

Reputation: 1043

Try taking a look at the CordaService Autopayroll sample on github.

link: https://github.com/corda/samples-java/tree/master/Features/cordaservice-autopayroll

There's an ability to access registered services that gets used here in the testing code

//Test #1 check if the requestState is being sent to the bank operator behind the scene.
@Test
fun `dummy test`() {
    val future = a.startFlow(RequestFlowInitiator("500", b.info.legalIdentities.first()))
    network.runNetwork()
    val ptx = future.get()
    println("Signed transaction hash: ${ptx.id}")
    listOf(a, bank).map {
        it.services.validatedTransactions.getTransaction(ptx.id)
    }.forEach {
        val txHash = (it as SignedTransaction).id
        println("$txHash == ${ptx.id}")
        assertEquals(ptx.id, txHash)
    }
}

link: https://github.com/corda/samples-kotlin/blob/master/Features/cordaService-autopayroll/workflows-kotlin/src/test/kotlin/net/corda/examples/autopayroll/FlowTests.kt

Good luck!

Upvotes: 1

Related Questions