marzelwidmer
05/27/2021, 6:27 PMSaharath Kleips
05/27/2021, 7:15 PMmarzelwidmer
05/27/2021, 7:35 PMmockk
https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/t[…]mework/web/reactive/function/client/WebClientExtensionsTests.kt when I compare it with my code I have to refactor I think… 🙂 I think I have again over myBook… 🙂 but thanks… to all….awaitBody
When I am not wrong is this the corutine
way of .bodyToMono
io.mockk.MockKException: no answer found for: ResponseSpec(#4).bodyToMono(ParameterizedTypeReference<class
thanksforallthefish
05/28/2021, 6:49 AMawaitBody<MyResponse>()
is an extension function and I think you cannot really mock those, as they don’t exist at runtime.
try to keep what you had before
coEvery { responseSpecMock.bodyToMono(MyResponse::class.java).hint(MyResponse::class) } returns response
ResponseSpec(#4)
on which you are calling a method bodyToMono
with a param, but you are not recording any answer for that interactionmarzelwidmer
05/28/2021, 9:49 PMexchangeFunction
:
😞 but the WebClient stuff looks more need for me…
@Test
fun `Should give back deckungGueltigAb value from lesefamileinvorstand`() {
val validiereFamilienvorstandResponseJson = File("src/test/resources/scenarios/validiereFamilienvorstandResponse.json").readText()
val webClient: WebClient = WebClient.builder().exchangeFunction {
Mono.just(
ClientResponse.create(HttpStatus.OK)
.header("Content-Type", "application/json")
.body(validiereFamilienvorstandResponseJson).build()
)
}.build()
// Service
val service = LeseFamilienvorstandService(webClient = webClient, serviceConfigurer = serviceConfigurer)
runBlocking {
StepVerifier
.create(service.validierePartnerCo(request).toMono())
.expectNext(response)
.expectComplete()
.verify()
}
}
@Test
fun `Should throw exception NOT_FOUND (404)`() {
val service = LeseFamilienvorstandService(webClient = webClientResponse404, serviceConfigurer = serviceConfigurer)
StepVerifier
.create(service.validierePartner(request))
.expectErrorMessage(EXCEPTION_MESSAGE_NOT_FOUND_404)
.verify()
}
@Test
fun `Should throw exception INTERNAL_SERVER_ERROR (500)`() {
val service = LeseFamilienvorstandService(webClient = webClientResponse500, serviceConfigurer = serviceConfigurer)
StepVerifier
.create(service.validierePartner(request))
.expectErrorMessage(EXCEPTION_MESSAGE_INTERNAL_SERVER_ERROR_500)
.verify()
}
BUT the sad part with Exception Test and coroutine
I get again some other issues….
suspend fun validierePartnerCo(request: ValidiereFamilienvorstandRequest): ValidiereFamilienvorstandResponse {
with(webClient) {
return post()
.uri(apiEndPoint())
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(request)
.retrieve()
.onStatus(httpStatusIs409ClientError()) { response -> Mono.error(PortalkontoBereitsVorhandenException()) }
.onStatus(httpStatusIs404ClientError()) { response -> Mono.error(PartnerNotFoundException()) }
.onStatus(HttpStatus::is4xxClientError) { response -> Mono.error(BadRequestException()) }
.onStatus(HttpStatus::is5xxServerError) { response -> Mono.error(UnexpectedServiceResponseException()) }
.bodyToMono(ValidiereFamilienvorstandResponse::class.java)
.awaitSingle()
.also {
logInfo(request, it)
}
}
}
fun validierePartner(request: ValidiereFamilienvorstandRequest): Mono<ValidiereFamilienvorstandResponse> {
with(webClient) {
return post()
.uri(apiEndPoint())
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(request)
.retrieve()
.onStatus(httpStatusIs409ClientError()) { response -> Mono.error(PortalkontoBereitsVorhandenException()) }
.onStatus(httpStatusIs404ClientError()) { response -> Mono.error(PartnerNotFoundException()) }
.onStatus(HttpStatus::is4xxClientError) { response -> Mono.error(BadRequestException()) }
.onStatus(HttpStatus::is5xxServerError) { response -> Mono.error(UnexpectedServiceResponseException()) }
// .awaitBody<ValidiereFamilienvorstandResponse>()
.bodyToMono(ValidiereFamilienvorstandResponse::class.java)
// .awaitSingle()
}
}
suspend
version of my impl. mytests fail.
runBlocking {
StepVerifier
.create(service.validierePartnerCo(request).toMono())
.expectErrorMessage(EXCEPTION_MESSAGE_NOT_FOUND_404)
.verify()
}
at |b|b|b(Coroutine boundary.|b(|b)
suspend
version wrong ? Or is there something mit my StepVerifier
I have to do in an other way ?
my impl. look for my correct… 😞 and I have only a problem to test my exception stuff …. or I miss undertand the awaitSingle()
or the mix with coroutine
and StepVerifier
is something.. not crisp 🙂Emil Kantis
05/29/2021, 9:45 AMMockWebServer
. Just my two cents 🙂marzelwidmer
05/29/2021, 12:01 PMIMHO
It have issues how I use the StepVerifier
together with coroutine
I changed now to Junit5
exception checks… but I have to check the MockWebServer
still…
@Test
fun `Should throw exception UnexpectedServiceResponseException for 500 for partnerNr basicUser_InternalServerError`() = runBlocking<Unit> {
shouldThrow<UnexpectedServiceResponseException> {
val service = LeseFamilienvorstandService(webClient = webClientResponse500, serviceConfigurer = serviceConfigurer)
service.validierePartner(request)
}
}
private val webClientResponse500 = WebClient.builder()
.exchangeFunction { Mono.just(ClientResponse.create(HttpStatus.INTERNAL_SERVER_ERROR).body(EXCEPTION_MESSAGE_INTERNAL_SERVER_ERROR_500).build()) }.build()
nicholasnet
05/30/2021, 2:53 PMmarzelwidmer
05/31/2021, 5:42 AMEmil Kantis
05/31/2021, 6:50 AMmarzelwidmer
05/31/2021, 6:55 PMTom Hermann
06/01/2021, 1:45 PM