如何在 Corda 4 中使用"模拟服务"进行单元测试?



在 Corda Enterprise 4.2 中,我正在尝试使用一些定制的 JPA 表来包装单元测试,并且以下代码出现问题:

serviceHub = MockServices(CordaX500Name.parse("O=OOO,L=Lambari,C=BR"))
serviceHub.withEntityManager{
val entityManager = this
test = CustomJPABackedClass() 
entityManager.persist(test)
}

此代码生成以下异常:

java.lang.UnsupportedOperationException
at net.corda.testing.node.MockServices.withEntityManager(MockServices.kt:412)
at project.support.CustomSupportTest$Companion.setupTest(CustomSupportTest.kt:70)
at project.support.CustomSupportTest.setupTest(CustomSupportTest.kt)
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$1.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.RunBefores.evaluate(RunBefores.java:24)
at org.hibernate.testing.junit4.BeforeClassCallbackHandler.evaluate(BeforeClassCallbackHandler.java:26)
at org.hibernate.testing.junit4.AfterClassCallbackHandler.evaluate(AfterClassCallbackHandler.java:25)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

如何正确使用此MockServices(或其他东西(来生成快速单元测试?

编辑

经过更多的研究,我在Corda 开源 4.2中找到了这些代码片段:

override fun jdbcSession(): Connection = throw UnsupportedOperationException()
override fun <T : Any> withEntityManager(block: EntityManager.() -> T): T {
throw UnsupportedOperationException()
}
override fun withEntityManager(block: Consumer<EntityManager>) {
throw UnsupportedOperationException()
}

现在,我知道我收到引用异常的原因。但问题仍然存在:我应该如何构建单元测试?我不想在创建用于测试的节点时等待 30 秒......

编辑

进一步的研究导致以下代码:

package project
import net.corda.core.identity.CordaX500Name
import net.corda.core.internal.packageName
import net.corda.nodeapi.internal.persistence.CordaPersistence
import net.corda.testing.common.internal.testNetworkParameters
import net.corda.testing.core.TestIdentity
import net.corda.testing.node.MockNetwork
import net.corda.testing.node.MockNetworkParameters
import net.corda.testing.node.MockServices
import net.corda.testing.node.internal.enclosedCordapp
import net.corda.testing.node.makeTestIdentityService
import org.junit.Before
import org.junit.Test
//import net.corda.testing.node.MockServices
class CustomTest  {
val myself = TestIdentity(CordaX500Name("Me", "London", "GB"))
lateinit var mockNet: MockNetwork
lateinit var services: MockServices
lateinit var database: CordaPersistence
@Before
fun setUp() {
mockNet = MockNetwork(MockNetworkParameters(cordappsForAllNodes = listOf(enclosedCordapp())))
val (db, mockServices) = MockServices.makeTestDatabaseAndMockServices(
cordappPackages = listOf(javaClass.packageName),
identityService = makeTestIdentityService(myself.identity),
initialIdentity = myself,
networkParameters = testNetworkParameters(minimumPlatformVersion = 4)
)
services = mockServices
database = db
}
@Test
fun actualTest() {
println("mockNet  : $mockNet")
println("services : $services")
println("database : $database")
}
}

以及以下例外情况:

java.lang.NoSuchMethodError: com.fasterxml.jackson.module.kotlin.KotlinModule.<init>(IZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
java.lang.NoSuchMethodError: com.fasterxml.jackson.module.kotlin.KotlinModule.<init>(IZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
at net.corda.client.jackson.JacksonSupport.configureMapper(JacksonSupport.kt:174)
at net.corda.client.jackson.JacksonSupport.createNonRpcMapper(JacksonSupport.kt:134)
at net.corda.client.jackson.JacksonSupport.createNonRpcMapper$default(JacksonSupport.kt:133)
at net.corda.node.services.rpc.CheckpointDumper.start(CheckpointDumper.kt:78)
at net.corda.node.internal.AbstractNode$start$8.invoke(AbstractNode.kt:425)
at net.corda.node.internal.AbstractNode$start$8.invoke(AbstractNode.kt:131)
at net.corda.nodeapi.internal.persistence.CordaPersistence.inTopLevelTransaction(CordaPersistence.kt:259)
at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:235)
at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:245)
at net.corda.node.internal.AbstractNode.start(AbstractNode.kt:405)
at net.corda.testing.node.internal.InternalMockNetwork$MockNode.start(InternalMockNetwork.kt:346)
at net.corda.testing.node.internal.InternalMockNetwork.createNodeImpl(InternalMockNetwork.kt:474)
at net.corda.testing.node.internal.InternalMockNetwork.createNode(InternalMockNetwork.kt:449)
at net.corda.testing.node.internal.InternalMockNetwork.createNode(InternalMockNetwork.kt:444)
at net.corda.testing.node.internal.InternalMockNetwork.createNotaries$node_driver(InternalMockNetwork.kt:253)
at net.corda.testing.node.internal.InternalMockNetwork.<init>(InternalMockNetwork.kt:236)
at net.corda.testing.node.internal.InternalMockNetwork.<init>(InternalMockNetwork.kt:149)
at net.corda.testing.node.MockNetwork.<init>(MockNetwork.kt:299)
at net.corda.testing.node.MockNetwork.<init>(MockNetwork.kt:288)
at net.corda.testing.node.MockNetwork.<init>(MockNetwork.kt:297)
at project.CustomTest.setUp(CustomTest.kt:43)
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$1.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.RunBefores.evaluate(RunBefores.java:24)
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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)    

对此有什么想法吗?

解决您的最后一个问题:我应该如何构建单元测试?

请随时参考我们的单元测试示例:此处。我们总是谈论CorDapp的开发采用测试驱动模式。在此示例中,您将学习如何编写测试用例。

但是,对于您的第二个问题,我认为Corda目前不支持Mockservices中的JPA表加载。因此,您可以加载任何预先存在的表。

最新更新