代码之家  ›  专栏  ›  技术社区  ›  Cate Daniel

Kotlin模拟Spring启动测试无法编译

  •  0
  • Cate Daniel  · 技术社区  · 6 年前

    所以我有以下测试

    @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)
    

    当该行被删除时,它会编译,但由于明显的原因测试失败。

    claimResponsive在测试类的顶部被注释为@MockBean,是一个JpaInterface。第217行是函数的开始。我也试着用 when 其他各种willReturn或willAnswer。。。。

    知道为什么吗?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Francesc    6 年前

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