Repository metrics
- Stars
- (5,540 stars)
- PR merge metrics
- (Avg merge 4d 7h) (7 merged PRs in 30d)
Description
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()) }
}
})