集成测试期间无法解析匿名方

Failed to resolve anonymous Party during integration test

我开发了一个简单的状态“项目”,表示卖家将出售给一群匿名买家的项目。我写了流程测试,一切似乎都很好。我还想实施集成测试以确认我的逻辑是否正常工作。由于卖方节点启动无法从其 public 键解析买方,因此我的流程失败了。我能够在没有任何自己的代码的情况下重现代码。我觉得我错过了一个注册步骤或其他什么,但是卖方节点可以再次使用 X500 名称解析买方节点。我有点困惑为什么我在通过 public 键解析买方节点时遇到问题。

要复制,请克隆 cordapps-kotlin-template 并将 DriverBasedTest.kt 的内容替换为以下代码 运行 gradlew integrationTest.

import net.corda.core.identity.AbstractParty
import net.corda.core.identity.CordaX500Name
import net.corda.core.utilities.getOrThrow
import net.corda.testing.core.TestIdentity
import net.corda.testing.driver.DriverDSL
import net.corda.testing.driver.DriverParameters
import net.corda.testing.driver.NodeHandle
import net.corda.testing.driver.driver
import net.corda.testing.node.User
import org.junit.Test
import kotlin.test.assertEquals

class AnonymousTest {
    private val seller = TestIdentity(CordaX500Name("Seller", "", "GB"))
    private val buyer = TestIdentity(CordaX500Name("Buyer", "", "US"))

    @Test
    fun `anonymous resolution test`() = withDriver {
        val sellerUser = User("sellerUser", "testPassword1", permissions = setOf("ALL"))
        val buyerUser = User("buyerUser", "testPassword1", permissions = setOf("ALL"))

        val sellerHandle = startNode(providedName = seller.name, rpcUsers = listOf(sellerUser)).getOrThrow()
        startNode(providedName = buyer.name, rpcUsers = listOf(buyerUser)).getOrThrow()
        val resolvedNameBuyerA = sellerHandle.resolveName(buyer.name)
        assertEquals(buyer.name, resolvedNameBuyerA)
        val resolveFromAnonymous = sellerHandle.resolveFromAnonymous(buyer.party.anonymise())
        assertEquals(buyer.name, resolveFromAnonymous)
    }

    // Runs a test inside the Driver DSL, which provides useful functions for starting nodes, etc.
    private fun withDriver(test: DriverDSL.() -> Unit) = driver(
            DriverParameters(isDebug = true, startNodesInProcess = true)
    ) { test() }

    // Makes an RPC call to retrieve another node's name from the network map.
    private fun NodeHandle.resolveName(name: CordaX500Name) = rpc.wellKnownPartyFromX500Name(name)!!.name

    private fun NodeHandle.resolveFromAnonymous(name: AbstractParty) = rpc.wellKnownPartyFromAnonymous(name)!!.name
}

测试结果如下:

kotlin.KotlinNullPointerException
    at com.template.AnonymousTest.resolveFromAnonymous(DriverBasedTest.kt:40)
    at com.template.AnonymousTest.access$resolveFromAnonymous(DriverBasedTest.kt:15)
    at com.template.AnonymousTest$anonymous resolution test.invoke(DriverBasedTest.kt:28)
    at com.template.AnonymousTest$anonymous resolution test.invoke(DriverBasedTest.kt:15)
    at com.template.AnonymousTest$withDriver.invoke(DriverBasedTest.kt:35)
    at com.template.AnonymousTest$withDriver.invoke(DriverBasedTest.kt:15)
    at net.corda.testing.node.internal.DriverDSLImplKt.genericDriver(DriverDSLImpl.kt:1289)
    at net.corda.testing.driver.Driver.driver(Driver.kt:197)
    at com.template.AnonymousTest.withDriver(DriverBasedTest.kt:33)
    at com.template.AnonymousTest.anonymous resolution test(DriverBasedTest.kt:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:412)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:48)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.lang.Thread.run(Thread.java:748)

请注意,Corda TestIdentity 可能会以这种方式产生误导,因为它们是新的且语义上独立的身份,即使您提供相同的名称也是如此。

很高兴听到你已经准备好了 Mustafa。祝你好运