From d398ac4393d99eb3591876602a05a5c29b972797 Mon Sep 17 00:00:00 2001 From: Oguzhan Soykan Date: Wed, 11 Dec 2024 15:34:02 +0100 Subject: [PATCH] docs: wiremock behavioral mocking --- docs/Components/04-wiremock.md | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/Components/04-wiremock.md b/docs/Components/04-wiremock.md index 6742589e..52db8a12 100644 --- a/docs/Components/04-wiremock.md +++ b/docs/Components/04-wiremock.md @@ -105,3 +105,42 @@ wiremock { Relative url is mocked. BaseUrl is known by Wiremock server since it hosts it, and your application because you passed it as a command line argument. + +### Behavioural Mocking + +Sometimes, a service call returns a failure response before a success response. You can define this behaviour with +behavioural mocking. + +```kotlin +test("behavioural tests") { + val expectedGetDtoName = UUID.randomUUID().toString() + TestSystem.validate { + wiremock { + behaviourFor("/get-behaviour", WireMock::get) { + initially { + aResponse() + .withStatus(503) + .withBody("Service unavailable") + } + then { + aResponse() + .withHeader("Content-Type", "application/json") + .withStatus(200) + .withBody(it.serialize(TestDto(expectedGetDtoName))) + } + } + } + http { + this.getResponse("/get-behaviour") { actual -> + actual.status shouldBe 503 + } + get("/get-behaviour") { actual -> + actual.name shouldBe expectedGetDtoName + } + } + } +} +``` + +Here we define a behaviour for the `/get-behaviour` endpoint. Initially, it returns a 503 status code with a message. +Then, it returns a 200 status code with a `TestDto` object.