Cate Daniel
Cate Daniel

Reputation: 744

Kotlin Mock Spring boot test can't compile

So I have the following test

@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), "[email protected]")

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

The line given(claimRepository.saveAll(anyList())).willAnswer { mutableListOf(daoClaim) } gives the folowing error

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)

When the line is removed, it compiles but the test fails for obvious reasons.

claimRespoitory is annotated @MockBean at the top of the test class, and is a JpaInterface. Line 217 is the start of the function. I've also trie using when and other various willReturn or willAnswer....

Any idea why?

Upvotes: 3

Views: 570

Answers (1)

Francesc
Francesc

Reputation: 11

I was facing the same issue when using Mockito any(). Changing it to ArgumentMatchers.any(YourClass::class.java)) solved the compilation error, there is a deprecated ArgumentMatchers.anyListOf(YourClass::class.java) that might do the job.

Upvotes: 1

Related Questions