Class WireMockRestServiceServer

java.lang.Object
org.springframework.cloud.contract.wiremock.WireMockRestServiceServer

public final class WireMockRestServiceServer extends Object
Convenience class for loading WireMock stubs into a MockRestServiceServer. In this way using a RestTemplate can mock the responses from a server using WireMock JSON DSL instead of the native Java DSL.
Author:
Dave Syer
  • Method Details

    • with

      public static WireMockRestServiceServer with(org.springframework.web.client.RestTemplate restTemplate)
      Public factory method for wrapping a rest template into a MockRestServiceServer.
      Parameters:
      restTemplate - the rest template to wrap
      Returns:
      a WireMockRestServiceServer
    • ignoreExpectOrder

      public WireMockRestServiceServer ignoreExpectOrder(boolean ignoreExpectOrder)
      Flag to tell the MockRestServiceServer to ignore the order of calls when matching requests. The default is true because there is an implied ordering in the stubs (by url path and with more specific request matchers first).
      Parameters:
      ignoreExpectOrder - flag value (default true)
      Returns:
      this
    • bufferContent

      public WireMockRestServiceServer bufferContent()
      Flag to tell the server to buffer the contents of the response.
      Returns:
      this
    • suffix

      public WireMockRestServiceServer suffix(String suffix)
      If stub locations are given as a directory, then we search recursively in that directory for files with this suffix. Default is ".json".
      Parameters:
      suffix - the suffix to use when creating a resource pattern
      Returns:
      this
    • baseUrl

      public WireMockRestServiceServer baseUrl(String baseUrl)
      Add a base url to all requests. Most WireMock JSON stubs have a path, but no protocol or host in the request matcher, so this is useful when your rest template is calling to a specific host.
      Parameters:
      baseUrl - a base url to apply
      Returns:
      this
    • stubs

      public WireMockRestServiceServer stubs(String... locations)
      Add some resource locations for stubs. Each location can be a resource path (to a single JSON file), or a pattern with ant-style wildcards to load all stubs that match, or a plain directory name (which will have **/*.json appended, where ".json" is the value of the suffix). Examples:
       classpath:/mappings/foo.json
       classpath:/mappings/*.json
       classpath:META-INF/com.example/stubs/1.0.0/mappings/**/*.json
       file:src/test/resources/stubs
       
      Parameters:
      locations - a set of resource locations
      Returns:
      this
    • files

      public WireMockRestServiceServer files(String... locations)
      Add some resource locations for files that represent response bodies. Wiremock defaults to "file:src/test/resources/__files".
      Parameters:
      locations - locations with files
      Returns:
      this
    • build

      public org.springframework.test.web.client.MockRestServiceServer build()
      Build a MockRestServiceServer from the configured stubs. The server can later be verified (optionally), if you need to check that all expected requests were made.
      Returns:
      a MockRestServiceServer