mockk/mockk

mockk library call verification fails for httpClient?

Open

#1,254 创建于 2024年5月21日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)Kotlin (364 fork)batch import
help wantedquestion

仓库指标

Star
 (5,540 star)
PR 合并指标
 (平均合并 4天 7小时) (30 天内合并 7 个 PR)

描述

Please answer the following questions for yourself before submitting an issue.

I am running the latest version I checked the documentation and found no answer I checked to make sure that this issue has not already been filed (pretty sure but not 100%)

Facing issue mocking httpclient: https://stackoverflow.com/questions/78505799/why-mockk-library-call-verification-fails-for-httpclient

I get the error message in the below test case. Any ideas?

java.lang.AssertionError: Verification failed: call 2 of 2: BlockingHttpClient(child of #1#3).retrieve(eq(POST /employee/create))) was not called I have mocked httpClient.toBlocking() method, so why it was not called?

@KafkaListener(offsetReset = OffsetReset.EARLIEST, groupId = "test")
class EmployeeListener(@Client("\${demo.restapi}") val client: HttpClient) {
    var log = KotlinLogging.logger {}
    val gson = GsonBuilder().setPrettyPrinting().create();

    @Topic("employee")
    fun recieveEmployee(str: String) {
        log.info { "Received employee creation request for $str" }

        try {
            val employeeDto  = gson.fromJson(str, EmployeeDto::class.java)

            val request: HttpRequest<Any> = HttpRequest.POST("/employee/create", employeeDto)
            val body = client.toBlocking().retrieve(request)
        } catch (e: JsonSyntaxException) {
            log.error ("Error parsing json", e)
        }

        log.info("Employee creation request sent successfully")
    }
}

// test class

    val httpClient = mockk<HttpClient>(relaxed = true)

    val blockingHttpClient = mockk<BlockingHttpClient>(relaxed = true)

    test("Employee listener test") {
        val listener = EmployeeListener(httpClient)

        val request: HttpRequest<Any> = HttpRequest.POST("/employee/create", EmployeeDto("Test1"))

        every { httpClient.toBlocking() } returns blockingHttpClient
        every { blockingHttpClient.retrieve(any()) } returns "{'name': 'Test1'}"

        listener.recieveEmployee("{'name': 'Test1'}")

        verify { httpClient.toBlocking().retrieve(request) }
    }

UPDATE: Below code also doesnt work

@MicronautTest
class EmployeeListenerTest: FunSpec( {

    val httpClient = mockk<HttpClient>(relaxed = true)

    val blockingHttpClient = mockk<BlockingHttpClient>(relaxed = true)

    test("Employee listener test") {
        val listener = EmployeeListener(httpClient)

        val request: HttpRequest<Any> = HttpRequest.POST("/employee/create", EmployeeDto("Test1"))

        every { httpClient.toBlocking().retrieve(any()) } returns "{'name': 'Test1'}"
        //every { blockingHttpClient.retrieve(any()) } returns "{'name': 'Test1'}"

        listener.recieveEmployee("{'name': 'Test1'}")

        verify { httpClient.toBlocking().retrieve(any()) }
    }

})

贡献者指南