【问题标题】:Kotlin Mock Spring boot test can't compileKotlin Mock Spring 启动测试无法编译
【发布时间】:2020-07-13 17:36:10
【问题描述】:

所以我有以下测试

@Test
    fun `when save claims is called with email saved to that profile` () {
        //arrange
        given(profileRepository.findProfileByEmail(anyString())).willAnswer { daoProfile }
        given(patientRepository.findById(anyInt())).willAnswer { Optional.of(daoPatient) }
        given(claimRepository.saveAll(anyList())).willAnswer { mutableListOf(daoClaim) }

        //act
        val result = claimService.saveClaimsForEmail(listOf(dtoClaim), "Test@test.test")

        //assert
        assert(result != null)
        assert(result?.isNotEmpty() ?: false)
        verify(claimRepository).saveAll(anyList())
    } 

given(claimRepository.saveAll(anyList())).willAnswer { mutableListOf(daoClaim) } 行给出以下错误

e: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Failed to generate expression: KtBlockExpression
File being compiled at position: (217,71) in /Users/archer/Work/masterhealth/master_billing/src/test/kotlin/com/masterhealthsoftware/master_billing/data/service/ClaimServiceTest.kt
The root cause java.lang.IllegalStateException was thrown at: org.jetbrains.kotlin.codegen.state.KotlinTypeMapper$typeMappingConfiguration$1.processErrorType(KotlinTypeMapper.kt:113)
...
Caused by: java.lang.IllegalStateException: Error type encountered: (???..???) (FlexibleTypeImpl).
        at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper$typeMappingConfiguration$1.processErrorType(KotlinTypeMapper.kt:113)

删除该行后,它会编译,但由于显而易见的原因测试失败。

claimRespoitory 在测试类的顶部被注解为@MockBean,并且是一个JpaInterface。第 217 行是函数的开始。我也尝试过使用when 和其他各种 willReturn 或 willAnswer....

知道为什么吗?

【问题讨论】:

  • 这看起来像一个 kotlin 编译器错误。您是否尝试过报告它和/或使用不同的编译器版本?
  • 不,我没有,我今天会看一下。我以为我首先做错了什么。
  • 你在使用最新的 kotlin 编译器吗?
  • 我用 1.3.71 试过了
  • 我开始认为这个问题来自于模拟 JpaRepository 方法。我正在尝试对 repository.findAll 进行模拟并得到相同的错误

标签: spring-boot kotlin mockito


【解决方案1】:

我在使用 Mockito any() 时遇到了同样的问题。将其更改为 ArgumentMatchers.any(YourClass::class.java)) 解决了编译错误,有一个已弃用的 ArgumentMatchers.anyListOf(YourClass::class.java) 可能会完成这项工作。

【讨论】:

  • 所以它不是任何匹配器。 anyList() 工作正常。我刚刚从尝试模拟其他东西中发现,在模拟 JpaRepository 方法时很麻烦。我有一个 findAll() 我试图模拟并得到同样的错误
  • 您能再解释一下吗?我在嘲笑findAll()findById(ArgumentMatchers.anyLong())findByLastName(ArgumentMatchers.anyStirng()) 和答案中提到的那个。使用JpaRepositoryReactiveCrudRepository 我没有收到任何编译错误。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 2018-11-27
相关资源
最近更新 更多