Alexandre A Barbosa
01/12/2023, 2:00 AMCould not inject field: private final <http://java.net|java.net>.http.HttpClient com.attune.ingestion.kafka.consumer.api.service.IngestionConsumerServiceTest.httpClient; nested exception is java.lang.IllegalStateException: The existing value '<http://jdk.internal.net|jdk.internal.net>.http.HttpClientImpl@668ea404(1)' of field 'private final <http://java.net|java.net>.http.HttpClient com.attune.ingestion.kafka.consumer.api.service.IngestionConsumerServiceTest.httpClient' is not the same as the new value 'HttpClient(<http://java.net|java.net>.http.HttpClient#0 bean#1)'
I would like to mock HttpClient using this instead of use WireMock:
@MockkBean val httpClient: HttpClient.Builder = HttpClient.newBuilder()
Please, any ideas to fix that and improves it, will be helpful.Sam
01/12/2023, 8:35 AMlateinit
property:
@MockkBean lateinit var httpClient: HttpClient
Davio
01/12/2023, 9:28 AM@MockkBean
with a concrete implementation, which is sillyDavio
01/12/2023, 9:30 AMAlexandre A Barbosa
01/12/2023, 11:31 AM@MockkBean lateinit var httpClient: HttpClient
I get:
lateinit property httpClient has not been initialized
Alexandre A Barbosa
01/12/2023, 11:32 AMhttpClientConfig
to the constructor here:
private val service: IngestionConsumerService = IngestionConsumerService(apiPathsConfig, httpClientConfig, 3L)
Alexandre A Barbosa
01/12/2023, 11:34 AM@MockBean
with the concrete implementation but I please, what would you do in this case to avoid of concrete implementation?Klitos Kyriacou
01/12/2023, 11:48 AMprivate val httpClientConfig = HttpClientConfig().apply { httpClient }
Your apply
function has no effect. You are just mentioning a variable httpClient
whose value is ignored because you are not assigning it to anything.Davio
01/12/2023, 12:23 PMAlexandre A Barbosa
01/12/2023, 12:24 PMAlexandre A Barbosa
01/12/2023, 12:25 PMAlexandre A Barbosa
01/12/2023, 12:25 PM@MockBean
and concrete implementationAlexandre A Barbosa
01/12/2023, 6:29 PM@ExtendWith(SpringExtension::class)
class IngestionConsumerServiceTest {
@MockkBean
lateinit var httpClient: HttpClient
@MockkBean
lateinit var httpResponse: HttpResponse<Any>
@Test
fun `Consuming from a valid protobuf topic message, sending to the ingestion service and waiting for Status CREATED`() {
every { httpResponse.statusCode() } returns 200
every { httpClient.send(any(), any<BodyHandler<Any>>()) } returns httpResponse
val httpRequestTimeout = 3L
val pathsMap = mapOf("__v2__uploads" to "<http://localhost:8085/>", "__api__v3__data" to "<http://localhost:8085/>")
val apiPathsConfig = ApiPathsConfig().apply { paths = pathsMap }
val service = IngestionConsumerService(apiPathsConfig, httpClient, httpRequestTimeout)
val ingestionHttpRequest = IngestionHttpRequest.HttpRequest.newBuilder()
.setMethod("post")
.setPath("/api/v3/data")
.setHttpVersion(HttpClient.Version.HTTP_1_1.toString())
.setHttpBody("YXR0dW5laW90".toByteStringUtf8())
.build()
val ingestionHttpRequestConsumer = ConsumerRecord(
"ingestion-topic", 1, 1, "ingestionKey", ingestionHttpRequest
)
service.consumeIngestion(ingestionHttpRequestConsumer)
val request = slot<HttpRequest>()
val requestHandler = slot<BodyHandler<Any>>()
verify { httpClient.send(capture(request), capture(requestHandler)) }
assertEquals(HttpMethod.POST.name, request.captured.method()) assertEquals(ingestionHttpRequest.httpBody.toByteArray(), listOf(request.captured.bodyPublisher().get()).get(0).boxedValue)
assertEquals(apiPathsConfig.paths[ingestionHttpRequest.path], request.captured.uri())
assertEquals(httpRequestTimeout, request.captured.timeout().get().seconds)
}
}
kqr
01/12/2023, 6:46 PMAlexandre A Barbosa
01/12/2023, 8:39 PMAlexandre A Barbosa
01/12/2023, 8:40 PM