@KafkaListener(offsetReset = OffsetReset.EARLIEST, groupId = “test”)
class EmployeeListener(@Client(“${demo.restapi}”) val client: HttpClient) {
<code>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")
}
</code>
<code>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")
}
</code>
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(relaxed = true)
<code>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) }
}
</code>
<code>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) }
}
</code>
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) }
}
I get the error message. I have mocked httpClient.toBlocking() method then why it was not called?
java.lang.AssertionError: Verification failed: call 2 of 2: BlockingHttpClient(child of #1#3).retrieve(eq(POST /employee/create))) was not called
New contributor
Bharat Jain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.