How to get Input states for flow testing

那年仲夏 提交于 2019-12-08 08:09:43

问题


I have two flows, say their names are:

  • flow_out (requires 1 input state)
  • flow_in (the above input state/transaction is stored by this)

My flow(flow_out) have 1 input state and 1 output state. The input state is retrieved from vault in the flow(flow_out) and the same is verified in contract by all the parties(Currrently 3 parties in test MockNetwork).

Now the test case is failing as my flow(flow_out) is unable to get that state, as that transaction never occurred(it's part of a different flow i.e flow_in).

To get around it, I initiated the other flow(flow_in) also in @Before of Junit, to store the transaction required for input state and everything passed.

What are some other ways available in Corda's flow testing APIs to store input transaction/states directly without running the flows only to store those input transacations?

Thanks for any help.


回答1:


Since you have access to the nodes' ServiceHubs, you can build, sign and store transactions directly in the test method, rather than using a flow:

class FlowTests {
    lateinit var network: MockNetwork
    lateinit var a: StartedMockNode
    lateinit var b: StartedMockNode

    @Before
    fun setup() {
        network = MockNetwork(listOf("com.example.contract"))
        a = network.createPartyNode()
        b = network.createPartyNode()
        listOf(a, b).forEach { it.registerInitiatedFlow(ExampleFlow.Acceptor::class.java) }
        network.runNetwork()
    }

    @After
    fun tearDown() {
        network.stopNodes()
    }

    @Test
    fun `a flow test`() {
        val lender = a.info.legalIdentities.first()
        val borrower = b.info.legalIdentities.first()

        val transactionBuilder = TransactionBuilder(network.defaultNotaryIdentity)
                .addOutputState(IOUState(99, lender, borrower), IOUContract.IOU_CONTRACT_ID)
                .addCommand(IOUContract.Commands.Create(), listOf(lender.owningKey, borrower.owningKey))

        a.transaction { transactionBuilder.verify(a.services) }
        val partSignedTransaction = a.services.signInitialTransaction(transactionBuilder)
        val signedTransaction = b.services.addSignature(partSignedTransaction)

        a.services.recordTransactions(signedTransaction)

        TODO("Test next flow.")
    }
}


来源:https://stackoverflow.com/questions/51491519/how-to-get-input-states-for-flow-testing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!