【问题标题】:Adding classes from src/test/kotlin to Corda MockNetwork nodes as a Cordapp将 src/test/kotlin 中的类作为 Cordapp 添加到 Corda MockNetwork 节点
【发布时间】:2019-10-27 01:45:01
【问题描述】:

有时,在我们的 Cordapp 测试代码中,我们需要注入额外的类(可能是额外的流、Corda 服务等)作为 Cordapp。以前在 Corda 3.x 中这是可能的。在 Corda 4.x 中似乎并非如此。

例子:

假设 kotlin 模板在测试源中有一个额外的类

/**
 * This service is representative of a service we use in our primary project.
 * It contains logic that's very useful from the perspective of our cordapps testing.
 * Historically in Corda 3.x we could load this service into the MockNetwork node's classpath
 * This is no longer the case. The log line in the initialiser is never called.
 */
@CordaService
class TestCordaService(serviceHub: AppServiceHub) : SingletonSerializeAsToken() {
//...
}

在 Corda 3.x 中,我们可以按如下方式启动网络:

MockNetwork(cordappPackages = listOf("<package names>"))

有没有办法在 Corda 4.x 中实现这一点。

【问题讨论】:

    标签: corda


    【解决方案1】:

    经过一些实验,我遇到了net.corda.testing.node.internal.CustomCordapp这个类。

    我们可以这样使用这个类:

      // we collect the distinct set of paths in the event that we don't add the same cordapp twice
      private val cordapps = listOf(
        TemplateContract::class,
        Initiator::class
    // and other key cordapp classes
      ).map { it.packageName }.distinct().map { TestCordapp.findCordapp(it) }
    
    // here we declare a custom cordapp based the entry-point classes in a given test package
      private val customTestCordapp = CustomCordapp(packages = setOf(TestCordaService::class.packageName),
        classes = setOf(TestCordaService::class.java))
    
      private val network = MockNetwork(MockNetworkParameters(cordappsForAllNodes = cordapps + customTestCordapp))
      // ... 
    

    请参阅此 repo 以获取示例:https://github.com/dazraf/mock-network-service-reproducer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2019-11-28
      • 2019-04-09
      • 2020-04-17
      • 2021-01-09
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多