Skip to content

Instantly share code, notes, and snippets.

@steklopod
Created December 19, 2018 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save steklopod/634d5bc55758ac61fdadd029119c8864 to your computer and use it in GitHub Desktop.
Save steklopod/634d5bc55758ac61fdadd029119c8864 to your computer and use it in GitHub Desktop.
Example of junit 5 WebTestClient on Kotlin
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.test.web.client.postForEntity
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.body
import reactor.core.publisher.Mono
import java.io.File
import java.nio.file.Files
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@AutoConfigureWebTestClient
internal class PayOperationTest(
@Autowired private val restTemplate: TestRestTemplate,
@Autowired private val webClient: WebTestClient,
@Autowired private val mapper: ObjectMapper
) {
lateinit var response: PayResponse
lateinit var payRequest: PayRequest
lateinit var request: OmniRequestItem<PayRequest>
lateinit var paymentOperation: PaymentOperation
lateinit var omniBusinessException: OmniBusinessException
val URI = "/api/v1/pay"
val URI_WITH_CONTEXT_PATH = "transfer-router/$URI"
val JSON_RESOURSCE_FOLDER = "json/transferstatus/"
@BeforeAll
fun setup() {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val fileResponse = ClassPathResource("${JSON_RESOURSCE_FOLDER}response.json").file
val fileRequest = ClassPathResource("${JSON_RESOURSCE_FOLDER}request.json").file
val fileError = ClassPathResource("${JSON_RESOURSCE_FOLDER}error.json").file
//Существуют ли файлы в папке с ресурсами:
assertTrue(fileRequest.exists()); assertTrue(fileResponse.exists()); assertTrue(fileError.exists())
val readerResp = mapper.readerFor(PayResponse::class.java)
val readerReq = mapper.readerFor(PayRequest::class.java)
val responseNode = mapper.readTree(toJsonString(fileResponse)).get("data")
val requestNode = mapper.readTree(toJsonString(fileRequest)).get("data")
omniBusinessException = mapper.readValue(toJsonString(fileError))
payRequest = readerReq.readValue(requestNode)
response = readerResp.readValue(responseNode)
paymentOperation = payRequest.paymentOperation
request = OmniRequestItem(OmniRequestMeta(channel = "OMNI"), PayRequest(paymentOperation))
println("\n omniBusinessException: $omniBusinessException")
println("paymentOperation: $paymentOperation")
println("payRequest: $payRequest")
println("response: $response \n")
}
@Test
fun check_Json_Request_From_File() {
val systemInfo = paymentOperation.systemInfo
assertEquals("OMNI", systemInfo.systemId)
assertEquals("a0eebc999c0b4ef8bb6d6bb9bd380a11", systemInfo.rawId)
assertEquals("A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11", paymentOperation.orderId)
}
@Test
fun check_Json_Response_From_File() {
val responseCode = response.status.code
assertEquals("processing", responseCode)
assertNotNull(response.status.name)
}
@Test
fun web_Client() {
webClient.post().uri(URI_WITH_CONTEXT_PATH)
.contentType(APPLICATION_JSON)
.body(Mono.just(request))
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("$.status").isEqualTo("message")
}
@Test
fun web_Client_2() {
webClient.post().uri(URI_WITH_CONTEXT_PATH)
.contentType(APPLICATION_JSON)
.body(Mono.just(request))
.exchange()
.expectStatus().isOk
.expectBody(OmniBusinessException::class.java)
}
@Test
fun check_Controller_When_Exception() {
val headers = HttpHeaders()
headers.set("Content-Type", APPLICATION_JSON_VALUE)
val requestWithHeaders = HttpEntity(payRequest, headers)
val response = restTemplate.postForEntity<OmniBusinessException>(URI, requestWithHeaders)
println(response)
assertEquals(omniBusinessException.exception, response.body!!.exception)
}
@Test
@Disabled
fun check_Class_Name() {
val clazz = PaymentOperation::class.java
val classNameDecapitalized = clazz.simpleName.decapitalize()
println(classNameDecapitalized)
assertEquals("paymentOperation", classNameDecapitalized)
}
}
internal fun toJsonString(fileResponse: File) = String(Files.readAllBytes(fileResponse.toPath()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment