Cordock与Mockito_kotlin进行单元测试 [英] Corda with mockito_kotlin for unit test

查看:80
本文介绍了Cordock与Mockito_kotlin进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟正在查询链下数据库/独立外部系统的链下/ API命中。但是,当我使用嘲笑者_kotlin模仿来自函数的响应时,我遇到了问题,似乎该部分在不同节点的启动期间并未被占用。

Hi I am trying to mock a Offchain/ API hit that is querying a offchain database / sepaarate external system. however i am facing problems when using mockito_kotlin to mock the response from the functions, it seems like the portion was not taken up during the starting of different nodes.

有没有示例或执行此操作的方法?

Is there any example or ways to perform this?

已编辑:

我有一个函数 callExternalService()将从offLedger服务返回一个字符串

I have a function callExternalService() which will return a string from offLedger service

class ExternalService {
  fun callExternalService() : String {
      // Call external service
      return externalResult
  }
}

我在执行流程之前使用Mockito模拟了该类,如下所示:

I mocked the class using Mockito before the execution of flow as below:

val externalService = Mockito.mock(ExternalService::class.java)
Mockito.doReturn("Hello").`when`(externalService.callExternalService()).toString()

我尝试在调用过程中调用模拟的 callExternalService()流与事r FinalityFlow 获取一些模拟数据。但是,在运行单元测试时,不会注入模拟的类,并且不会从类中返回 Hello

I have tried to call to the mocked callExternalService() during the flow and after the FinalityFlow to get some mock data. However when the unit test runs, the mocked class is not injected and the "Hello" is not returned from the class.

因此,我我怀疑在基础起始节点序列期间模拟功能未带入节点,因为它仍在尝试访问外部服务,而我不想出于单元测试目的而使用其他外部系统。

Hence, i was suspecting that the mock function is not taken into the nodes during the base start-nodes sequence, as it was still trying to hit an external service where I do not want to involve another external system for unit test purposes.

推荐答案

假设您具有以下服务:

@CordaService
class ExternalService: SingletonSerializeAsToken() {
    fun callExternalService() : String {
        val externalResult = TODO("Call external service")
        return externalResult
    }
}

您需要将对服务的调用拉到另一个位置在您的流程中发挥作用。使此单独的函数和流类本身打开,以便可以覆盖它们:

You'd need to pull the call to the service out into a separate function in your flow. Make this separate function and the flow class itself open, so that they can be overridden:

@InitiatingFlow
@StartableByRPC
open class RealFlow : FlowLogic<String>() {
    @Suspendable
    override fun call(): String {
        return queryService()
    }

    @Suspendable
    open fun queryService(): String {
        return serviceHub.cordaService(ExternalService::class.java).callExternalService()
    }
}

现在创建流的测试子类以覆盖 queryService 返回虚拟数据而不是调用API:

Now create a test subclass of the flow that overrides queryService to return dummy data instead of calling out to an API:

class TestFlow : RealFlow() {
    override fun queryService(): String {
        return "dummyData"
    }
}

您现在可以在流测试中使用 TestFlow 来测试主流的功能:

You can now use TestFlow in flow tests to test the functionality of the main flow:

@Test
fun `example test`() {
    val flow = TestFlow()
    val future = node.services.startFlow(flow).resultFuture
    network.runNetwork()
    val externalResult = future.getOrThrow()
    assertEquals("dummyData", externalResult)
}

这篇关于Cordock与Mockito_kotlin进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆