Show List
Wiremock Coding Questions
- Write a WireMock stub that returns a JSON response with a status code of 200.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that returns a response with a custom HTTP status code.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(555)
.withBody("Custom status code")));
- Write a WireMock stub that returns a response with a dynamic header value based on the request URL.
lessCopy code
stubFor(get(urlMatching("/api/.*"))
.willReturn(aResponse()
.withHeader("X-Endpoint", "${request.url}")
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that returns a response with a delay of 5 seconds.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")
.withFixedDelay(5000)));
- Write a WireMock stub that returns a response with a randomized delay of up to 10 seconds.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")
.withUniformRandomDelay(0, 10000)));
- Write a WireMock stub that returns a response with a header that contains a placeholder value.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("X-Request-Id", "{{request.requestLine.uri}}")
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that returns a response with a header that contains a UUID.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("X-Request-Id", "{{randomValue type='UUID'}}")
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that returns a response with a JSON body that contains a list of items.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBody("{\"items\": [\"item1\", \"item2\", \"item3\"]}")));
- Write a WireMock stub that returns a response with a JSON body that contains a nested object.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBody("{\"user\": {\"name\": \"John Doe\", \"age\": 30}}")));
- Write a WireMock stub that returns a response with a JSON body that contains a random number between 1 and 100.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBody("{\"randomNumber\": {{randomValue type='NUMBER' min=1 max=100}} }")));
- Write a WireMock stub that matches requests with a custom HTTP header.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.withHeader("X-Custom-Header", containing("value"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that matches requests with a query parameter and returns a custom response.
lessCopy code
stubFor(get(urlPathEqualTo("/api/test"))
.withQueryParam("param", equalTo("value"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that matches requests with a regular expression in the URL and returns a custom response.
lessCopy code
stubFor(get(urlMatching("/api/test/.+"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that matches requests with a specific HTTP method and returns a custom response.
lessCopy code
stubFor(post(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that matches requests with a specific request body and returns a custom response.
lessCopy code
stubFor(post(urlEqualTo("/api/test"))
.withRequestBody(equalToJson("{\"name\": \"John Doe\"}"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that matches requests with a specific request header and returns a custom response.
lessCopy code
stubFor(post(urlEqualTo("/api/test"))
.withHeader("Content-Type", equalTo("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"message\": \"success\"}")));
- Write a WireMock stub that returns a response with a file as the response body.
lessCopy code
File file = new File("src/test/resources/example.json");
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile(file)));
- Write a WireMock stub that returns a response with a file as the response body and a dynamic header value.
lessCopy code
File file = new File("src/test/resources/example.json");
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile(file)
.withHeader("X-Request-Id", "{{request.url}}")));
- Write a WireMock stub that returns a response with a file as the response body and a delay of 2 seconds.
lessCopy code
File file = new File("src/test/resources/example.json");
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile(file)
.withFixedDelay(2000)));
- Write a WireMock stub that returns a response with a JSON body that contains a randomized date value.
lessCopy code
stubFor(get(urlEqualTo("/api/test"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBody("{\"date\": {{date type='ANY' format='yyyy-MM-dd'}}}")));
Leave a Comment