Unit testing 如何在Corda 4中使用“MockServices”进行单元测试?
在Corda Enterprise 4.2中,我试图使用一些定制的JPA表结束一个单元测试,但我对以下代码有问题:Unit testing 如何在Corda 4中使用“MockServices”进行单元测试?,unit-testing,testing,automated-tests,corda,Unit Testing,Testing,Automated Tests,Corda,在Corda Enterprise 4.2中,我试图使用一些定制的JPA表结束一个单元测试,但我对以下代码有问题: serviceHub = MockServices(CordaX500Name.parse("O=OOO,L=Lambari,C=BR")) serviceHub.withEntityManager{ val entityManager = this test = CustomJPABackedClass() ent
serviceHub = MockServices(CordaX500Name.parse("O=OOO,L=Lambari,C=BR"))
serviceHub.withEntityManager{
val entityManager = this
test = CustomJPABackedClass()
entityManager.persist(test)
}
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.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)
java.lang.NoSuchMethodError: com.fasterxml.jackson.module.kotlin.KotlinModule.<init>(IZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
如何正确使用此MockServices
(或其他)来生成快速单元测试
编辑
经过进一步研究,我在Corda OpenSource 4.2中发现了以下代码片段:
除以下情况外:
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)
java.lang.NoSuchMethodError: com.fasterxml.jackson.module.kotlin.KotlinModule.<init>(IZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
java.lang.NoSuchMethodError:com.fasterxml.jackson.module.kotlin.KotlinModule.(IZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
java.lang.NoSuchMethodError:com.fasterxml.jackson.module.kotlin.KotlinModule.(IZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
位于net.corda.client.jackson.JacksonSupport.configureMapper(JacksonSupport.kt:174)
在net.corda.client.jackson.JacksonSupport.createNonRpcMapper上(JacksonSupport.kt:134)
位于net.corda.client.jackson.JacksonSupport.createNonRpcMapper$default(JacksonSupport.kt:133)
位于net.corda.node.services.rpc.CheckpointDumper.start(CheckpointDumper.kt:78)
net.corda.node.internal.AbstractNode$start$8.invoke(AbstractNode.kt:425)
位于net.corda.node.internal.AbstractNode$start$8.invoke(AbstractNode.kt:131)
位于net.corda.nodeapi.internal.persistence.CordaPersistence.inTopLevelTransaction(CordaPersistence.kt:259)
net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:235)
位于net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:245)
在net.corda.node.internal.AbstractNode.start(AbstractNode.kt:405)处
位于net.corda.testing.node.internal.InternalMockNetwork$MockNode.start(InternalMockNetwork.kt:346)
位于net.corda.testing.node.internal.InternalMockNetwork.createNodeImpl(InternalMockNetwork.kt:474)
位于net.corda.testing.node.internal.InternalMockNetwork.createNode(InternalMockNetwork.kt:449)
位于net.corda.testing.node.internal.InternalMockNetwork.createNode(InternalMockNetwork.kt:444)
在net.corda.testing.node.internal.InternalMockNetwork.create公证人$node\u驱动程序(InternalMockNetwork.kt:253)
net.corda.testing.node.internal.InternalMockNetwork.(InternalMockNetwork.kt:236)
net.corda.testing.node.internal.InternalMockNetwork.(InternalMockNetwork.kt:149)
位于net.corda.testing.node.MockNetwork.(MockNetwork.kt:299)
在net.corda.testing.node.MockNetwork.(MockNetwork.kt:288)
在net.corda.testing.node.MockNetwork.(MockNetwork.kt:297)
在project.CustomTest.setUp(CustomTest.kt:43)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:363)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:137)
位于com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
位于com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
位于com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
位于com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
对此有何想法?来回答您的最后一个问题:我应该如何构建单元测试 请随时参阅我们的单元测试样本:。我们总是说CorDapp的开发采用测试驱动模式。在本例中,您将了解如何编写测试用例 但是,关于第二个问题,我认为Corda目前不支持
Mockservices
中的JPA表加载。因此,您可以加载任何预先存在的表