将src/test/kotlin中的类作为Cordapp添加到Corda MockNetwork节点
有时,在我们的Cordapp测试代码中,我们需要将其他类(可能是附加流、Corda服务等)作为Cordapp注入。以前在Corda 3.x中,这是可能的。在Corda 4.x中,情况似乎并非如此 例如: 给定kotlin模板,假设测试源中有一个额外的类用于将src/test/kotlin中的类作为Cordapp添加到Corda MockNetwork节点,corda,Corda,有时,在我们的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
/**
* 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>"))
MockNetwork(cordappPackages=listOf(“”)
有没有办法在Corda 4.x中实现这一点。经过一些实验后,我遇到了类
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))
// ...
查看此报告以获取示例:经过一些实验后,我遇到了类
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))
// ...
有关示例,请参见本回购协议: