gpt4 book ai didi

java - Kotlin - Mockito 测试失败

转载 作者:行者123 更新时间:2023-12-02 01:50:38 28 4
gpt4 key购买 nike

我的 kotlin 代码库中有以下方法

package com.service.organization

import com.domain.Organization
import com.repository.organization.OrganizationRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

interface OrganizationService {
fun listAll(pageable : Pageable , archived : Boolean?) : Page<Organization>
}

@Service
internal class OrganizationServiceImpl(private val organizationRepository : OrganizationRepository) : OrganizationService {

@Transactional(readOnly = true)
override fun listAll(pageable : Pageable , archived : Boolean?) : Page<Organization> {
val organizations : Page<Organization>
if (archived !== null) {
organizations = organizationRepository.findByArchived(archived , pageable)
} else {
organizations = organizationRepository.findAll(pageable)
}
return organizations
}
}

我有以下测试来测试存档流程

package com.service.organization

import com.domain.Organization
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnitRunner
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import java.util.*

@RunWith(MockitoJUnitRunner::class)
class OrganizationServiceTest {

@Mock
lateinit var mockOrganizationRepository: OrganizationRepository

@Mock
lateinit var mockPageable: Pageable

internal lateinit var organizationServiceImpl: OrganizationServiceImpl

@Before
fun setUp() {
organizationServiceImpl = OrganizationServiceImpl(mockOrganizationRepository)
//mockPageable = Mockito.mock(Pageable::class.java)
}

@Test
fun testListAll_Archived_NoOrgsReturned() {
val archived = true
var mockPageableObj = Mockito.mock(Pageable::class.java)
val organizationList = LinkedList<Organization>()
val returnedPage = PageImpl(organizationList)

var exc: Exception? = null
try {
Mockito.`when`(mockOrganizationRepository.findByArchived(Mockito.anyBoolean(), Mockito.isA(Pageable::class.java))).thenReturn(returnedPage)

val organizationPage = organizationServiceImpl.listAll(mockPageableObj, archived)
assertNotNull(organizationPage)
assertTrue(organizationPage.size == returnedPage.size)

Mockito.verify(mockOrganizationRepository, Mockito.times(1)).findByArchived(Mockito.anyBoolean(), Mockito.isA(Pageable::class.java))
} catch(e: Exception) {
exc = e
exc.printStackTrace()
}
assertNotNull(exc)
}

}

但是,当我运行该测试时 - 我收到以下错误

[INFO] Running com.service.organization.OrganizationServiceTest
java.lang.IllegalStateException: Mockito.isA(Pageable::class.java) must not be null
at com.service.organization.OrganizationServiceTest.testListAll_Archived_NoOrgsReturned(OrganizationServiceTest.kt:139)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)

有人知道我在这里做错了什么吗?

提前致谢

最佳答案

我没有使用匹配器,而是使用了方法调用的特定值。

更新

这篇中等文章还强调了一个有效的解决方案,您可以在其中传递 any() 匹配器 https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791

关于java - Kotlin - Mockito 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57428994/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com