WireMock and Spring MVC simulator
Spring Cloud Contract class provides a convenient, JSON WireMock Spring stub can be loaded into MockRestServiceServer
the. Here is an example:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class WiremockForDocsMockServerApplicationTests {
@Autowired
private RestTemplate restTemplate;
@Autowired
private Service service;
@Test
public void contextLoads() throws Exception {
// will read stubs classpath
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
.baseUrl("http://example.org").stubs("classpath:/stubs/resource.json")
.build();
// We're asserting if WireMock responded properly
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}
}
baseUrl
In front of all analog calls, stubs()
method a stub resource path mode as arguments. Therefore, in this example, /stubs/resource.json
defined in the stub is loaded into the analog server, if RestTemplate
the required access http://example.org/
, it will get the response declared. Stubs can specify multiple modes, each of which may be a directory (for ".json" a list of all recursive) or a fixed file name (in the above example), or an ant style pattern. JSON format is the usual WireMock format, you can read on WireMock website.
Currently, we support Tomcat, Jetty and Undertow as Spring Boot embedded server, while Wiremock itself has a "native" support for specific versions of Jetty (currently 9.2). To use a local Jetty, you need to add native thread dependencies, and the exclusion of Spring Boot container (if any).
Use RestDocs generate stubs
Spring RestDocs can be used to generate the HTTP API documentation (e.g., asciidoctor format) having the Spring MockMvc or RestEasy. While generating API documentation, you can also use Spring Cloud Contract WireMock generate WireMock stub. Just write normal RestDocs test cases and use @AutoConfigureRestDocs
in restdocs output directory automatically stored stub. E.g:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureRestDocs(outputDir = "target/snippets")
@AutoConfigureMockMvc
public class ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void contextLoads() throws Exception {
mockMvc.perform(get("/resource"))
.andExpect(content().string("Hello World"))
.andDo(document("resource"));
}
}
From this test will be generated on a stub WireMock "target / snippets / stubs / resource.json ". It all GET request "/ resource" to match the path.
No other configuration, this will create a stub matcher requests and HTTP method, and all except the first "host" and "content length". In order to more accurately match the request, for example, to match the body of the POST or PUT, we need to create a clear request matcher. This will do two things: 1) create a matching stub only way that you specify, and 2) request assertion test case also match the same conditions.
The main entry point is WireMockRestDocs.verify()
, you can substitute document()
a convenient method. E.g:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureRestDocs(outputDir = "target/snippets")
@AutoConfigureMockMvc
public class ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void contextLoads() throws Exception {
mockMvc.perform(post("/resource")
.content("{\"id\":\"123456\",\"message\":\"Hello World\"}"))
.andExpect(status().isOk())
.andDo(verify().jsonPath("$.id")
.stub("resource"));
}
}
So this contract is to say: Any valid POST and "id" field will get the same response in this test. You can link to the call .jsonPath()
to add another matcher. If you are not familiar with JayWay documentation can help speed up your JSON path.
You can also use WireMock API to verify whether the request matches the stub created, instead of using jsonPath
and contentType
methods. example:
@Test
public void contextLoads() throws Exception {
mockMvc.perform(post("/resource")
.content("{\"id\":\"123456\",\"message\":\"Hello World\"}"))
.andExpect(status().isOk())
.andDo(verify()
.wiremock(WireMock.post(
urlPathEquals("/resource"))
.withRequestBody(matchingJsonPath("$.id"))
.stub("post-resource"));
}
WireMock API is rich - you can match the regular expression header files and json path, query parameters, and request body, so it can be used to create a stub with a wider range of parameters. The above example will generate a this stub:
{
"request" : {
"url" : "/resource",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.id"
}]
},
"response" : {
"status" : 200,
"body" : "Hello World",
"headers" : {
"X-Application-Context" : "application:-1",
"Content-Type" : "text/plain"
}
}
}
note
|
You can use the wiremock() method or jsonPath() and contentType() create request matcher method, but not both. |
, Assumed in consumption generated above resource.json
can be used in the classpath, WireMock you can use a number of different ways to create a stub, which comprises the use of the above @AutoConfigureWireMock(stubs="classpath:resource.json")
description.