如何在使用 mockito 模拟测试时解决 NullpointerException?
How to solve NullpointerException while testing mocked using mockito?
如何解决 NullpointerException nodeInfoList.map { it.legalIdentities.last().name } where nodeInfoList is of type while testing mocked using mockito?
##待测服务代码##
val nodeInfoList=dataService.proxy.networkMapSnapshot()
val nodeNameList =nodeInfoList.map { it.legalIdentities.last().name }
ownerMap.map {
if(nodeNameList.contains(CordaX500Name.parse(it.value))) {
ownerList.add(it.key)
}
}
maintanenceMap.map {
if(nodeNameList.contains(CordaX500Name.parse(it.value))) {
maintainerList.add(it.key)
}
}
response["ownerList"] = ownerList
response["maintainerList"] = maintainerList
return ResponseEntity.status(200).body(response)
}
#我写的测试函数#
@Test(timeout = 300000)
fun ` get drop down data Test`() {
val cordaRPCOps = Mockito.mock(CordaRPCOps::class.java)
val listNodeInfoMock = Mockito.mock(listOf<net.corda.core.node.NodeInfo>()::class.java)
val singleMockNodeInfo = Mockito.mock(net.corda.core.node.NodeInfo::class.java)
val x500Name = CordaX500Name.parse("O=Building Owner1,L=Tokyo,C=JP")
val list500Mock2 = listOf(x500Name)
Mockito.`when`(cordaDataService!!.proxy).thenReturn(cordaRPCOps)
Mockito.`when`(cordaRPCOps.networkMapSnapshot()).thenReturn(listNodeInfoMock)
Mockito.`when`(listNodeInfoMock.map { it.legalIdentities.last().name }).thenReturn(list500Mock2)
Mockito.`when`(list500Mock2.contains(CordaX500Name.parse("O=Building Owner1,L=Tokyo,C=JP"))).thenReturn(true)
val dropDownData = staticDataService!!.getDropDownData()
println("dropDownData " + dropDownData)
}
我认为这取决于您的测试目标,您是否希望该列表为空以及是否要测试它。一般来说,有很多方法可以用 Mockito 处理空列表,例如,你可以看到这个 other post,看看哪种方法最适合你的目的。
fun ` list data Test`() {
val x500Name = CordaX500Name.parse("O=Maintenance Operator1,L=Tokyo,C=JP")
val partyMock = Mockito.mock(Party::class.java)
Mockito.`when`(cordaDataService!!.proxy).thenReturn(proxyMock)
Mockito.`when`(proxyMock!!.networkMapSnapshot()).thenReturn(listNodeInfoMock)
Mockito.`when`(listNodeInfoMock!!.iterator()).thenReturn(nodeInfoIterator)
Mockito.`when`(nodeInfoIterator!!.hasNext()).thenReturn(true,false)
Mockito.`when`(nodeInfoIterator.next()).thenReturn(nodeInfoMock).thenReturn(nodeInfoMock2)
Mockito.`when`(nodeInfoMock!!.legalIdentities ).thenReturn(listOf(partyMock))
Mockito.`when`(nodeInfoMock.legalIdentities.last().name ).thenReturn(x500Name)
Mockito.`when`(list500Mock2!!.contains(x500Name)).thenReturn(true)
}
Rewritten test function like this. Nullpointer exception solved.Used @Mock for some of the mockobjects used here.
@InjectMocks
private val staticDataService: StaticDataService? = null
@Mock
private val cordaDataService: CordaDataService? = null
@Mock
private val proxyMock: CordaRPCOps? = null
@Mock
private val listNodeInfoMock: List<NodeInfo>? = null
如何解决 NullpointerException nodeInfoList.map { it.legalIdentities.last().name } where nodeInfoList is of type while testing mocked using mockito?
##待测服务代码##
val nodeInfoList=dataService.proxy.networkMapSnapshot()
val nodeNameList =nodeInfoList.map { it.legalIdentities.last().name }
ownerMap.map {
if(nodeNameList.contains(CordaX500Name.parse(it.value))) {
ownerList.add(it.key)
}
}
maintanenceMap.map {
if(nodeNameList.contains(CordaX500Name.parse(it.value))) {
maintainerList.add(it.key)
}
}
response["ownerList"] = ownerList
response["maintainerList"] = maintainerList
return ResponseEntity.status(200).body(response)
}
#我写的测试函数#
@Test(timeout = 300000)
fun ` get drop down data Test`() {
val cordaRPCOps = Mockito.mock(CordaRPCOps::class.java)
val listNodeInfoMock = Mockito.mock(listOf<net.corda.core.node.NodeInfo>()::class.java)
val singleMockNodeInfo = Mockito.mock(net.corda.core.node.NodeInfo::class.java)
val x500Name = CordaX500Name.parse("O=Building Owner1,L=Tokyo,C=JP")
val list500Mock2 = listOf(x500Name)
Mockito.`when`(cordaDataService!!.proxy).thenReturn(cordaRPCOps)
Mockito.`when`(cordaRPCOps.networkMapSnapshot()).thenReturn(listNodeInfoMock)
Mockito.`when`(listNodeInfoMock.map { it.legalIdentities.last().name }).thenReturn(list500Mock2)
Mockito.`when`(list500Mock2.contains(CordaX500Name.parse("O=Building Owner1,L=Tokyo,C=JP"))).thenReturn(true)
val dropDownData = staticDataService!!.getDropDownData()
println("dropDownData " + dropDownData)
}
我认为这取决于您的测试目标,您是否希望该列表为空以及是否要测试它。一般来说,有很多方法可以用 Mockito 处理空列表,例如,你可以看到这个 other post,看看哪种方法最适合你的目的。
fun ` list data Test`() {
val x500Name = CordaX500Name.parse("O=Maintenance Operator1,L=Tokyo,C=JP")
val partyMock = Mockito.mock(Party::class.java)
Mockito.`when`(cordaDataService!!.proxy).thenReturn(proxyMock)
Mockito.`when`(proxyMock!!.networkMapSnapshot()).thenReturn(listNodeInfoMock)
Mockito.`when`(listNodeInfoMock!!.iterator()).thenReturn(nodeInfoIterator)
Mockito.`when`(nodeInfoIterator!!.hasNext()).thenReturn(true,false)
Mockito.`when`(nodeInfoIterator.next()).thenReturn(nodeInfoMock).thenReturn(nodeInfoMock2)
Mockito.`when`(nodeInfoMock!!.legalIdentities ).thenReturn(listOf(partyMock))
Mockito.`when`(nodeInfoMock.legalIdentities.last().name ).thenReturn(x500Name)
Mockito.`when`(list500Mock2!!.contains(x500Name)).thenReturn(true)
}
Rewritten test function like this. Nullpointer exception solved.Used @Mock for some of the mockobjects used here.
@InjectMocks
private val staticDataService: StaticDataService? = null
@Mock
private val cordaDataService: CordaDataService? = null
@Mock
private val proxyMock: CordaRPCOps? = null
@Mock
private val listNodeInfoMock: List<NodeInfo>? = null