Skip to content

Commit

Permalink
docs: wiremock behavioral mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Dec 11, 2024
1 parent 49899ff commit d398ac4
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/Components/04-wiremock.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestDto>("/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.

0 comments on commit d398ac4

Please sign in to comment.