Skip to content

Commit

Permalink
docs(wiremock)
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Dec 10, 2024
1 parent 4b73d4a commit f808469
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions docs/Components/04-wiremock.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,82 @@ TestSystem()
}
.run()
```

### Options

```kotlin
data class WireMockSystemOptions(
/**
* Port of wiremock server
*/
val port: Int = 9090,
/**
* Configures wiremock server
*/
val configure: WireMockConfiguration.() -> WireMockConfiguration = { this.notifier(ConsoleNotifier(true)) },
/**
* Removes the stub when request matches/completes
* Default value is false
*/
val removeStubAfterRequestMatched: Boolean = false,
/**
* Called after stub removed
*/
val afterStubRemoved: AfterStubRemoved = { _, _ -> },
/**
* Called after request handled
*/
val afterRequest: AfterRequestHandler = { _, _ -> },
/**
* ObjectMapper for serialization/deserialization
*/
val serde: StoveSerde<Any, ByteArray> = StoveSerde.jackson.anyByteArraySerde()
) : SystemOptions
```

## Mocking

Wiremock starts a mock server on the `localhost` with the given port. The important thing is that you use the same port
in your application for your services.

Say, your application calls an external service in your production configuration as:
`http://externalservice.com/api/product/get-all`
you need to replace the **base url** of this an all the external services with the Wiremock host and port:
`http://localhost:9090`

You can either do this in your application configuration, or let Stove send this as a command line argument to your
application.

```kotlin
TestSystem()
.with {
wiremock {
WireMockSystemOptions(
port = 9090,
)
}
springBoot( // or ktor
runner = {
// ...
},
withParameters = listOf(
"externalServiceBaseUrl=http://localhost:9090",
"otherService1BaseUrl=http://localhost:9090",
"otherService2BaseUrl=http://localhost:9090"
)
)
}
.run()
```

All service endpoints will be pointing to the Wiremock server. You can now define the stubs for the services that your
application calls.

```kotlin
wiremock {
mockGet("/api/product/get-all", 200, lisOf(Product("1", "Product 1"), Product("2", "Product 2")).some())
}
```

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.

0 comments on commit f808469

Please sign in to comment.