Show List

Wiremock Coding Questions

  • Write a WireMock stub that returns a JSON response with a status code of 200.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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.
less
Copy 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


  • captcha text