WireMock RestDocs stub() method is a no-op

deprecating the method and going towards document()
fix by @dsyer

fixes gh-1092
This commit is contained in:
Marcin Grzejszczak
2019-11-05 14:03:22 +01:00
parent 4e375b99e1
commit 380d2535bd
9 changed files with 48 additions and 28 deletions

View File

@@ -269,8 +269,8 @@ public class ApplicationTests {
mockMvc.perform(post("/resource")
.content("{\"id\":\"123456\",\"message\":\"Hello World\"}"))
.andExpect(status().isOk())
.andDo(verify().jsonPath("$.id")
.stub("resource"));
.andDo(verify().jsonPath("$.id"))
.andDo(document("resource"));
}
}
----
@@ -295,8 +295,8 @@ following example:
.andDo(verify()
.wiremock(WireMock.post(
urlPathEquals("/resource"))
.withRequestBody(matchingJsonPath("$.id"))
.stub("post-resource"));
.withRequestBody(matchingJsonPath("$.id")))
.andDo(document("post-resource"));
}
----

View File

@@ -22,6 +22,15 @@
<include>**/model/Fraud*.*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/generated-snippets/stubs</directory>
<outputDirectory>
META-INF/${project.groupId}/${project.artifactId}/${project.version}/mappings
</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/snippets/stubs</directory>
<outputDirectory>

View File

@@ -36,6 +36,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocs.verify;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@RunWith(SpringRunner.class)
@@ -69,8 +70,8 @@ public class StubGeneratorTests {
.andExpect(jsonPath("$.rejectionReason").value("Amount too high"))
.andDo(verify().jsonPath("$.clientId")
.jsonPath("$[?(@.loanAmount > 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.stub("markClientAsFraud"));
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json")))
.andDo(document("markClientAsFraud"));
}
@Test
@@ -85,8 +86,8 @@ public class StubGeneratorTests {
.andExpect(jsonPath("$.rejectionReason").doesNotExist())
.andDo(verify().jsonPath("$.clientId")
.jsonPath("$[?(@.loanAmount <= 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.stub("markClientAsNotFraud"));
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json")))
.andDo(document("markClientAsNotFraud"));
}
}

View File

@@ -47,8 +47,7 @@ import org.springframework.restdocs.snippet.Snippet;
* // first WireMock
* .andDo(WireMockRestDocs.verify()
* .jsonPath("$[?(&#64;.foo >= 20)]")
* .contentType(MediaType.valueOf("application/json"))
* .stub("shouldGrantABeerIfOldEnough"))
* .contentType(MediaType.valueOf("application/json")))
* // then Contract DSL documentation
* .andDo(document("index", SpringCloudContractRestDocs.dslContract()));
* }

View File

@@ -33,7 +33,8 @@ package org.springframework.cloud.contract.wiremock.restdocs;
* public void contextLoads() throws Exception {
* mockMvc.perform(get("/resource"))
* .andExpect(content().string("Hello World"))
* .andDo(verify().stub("resource"));
* .andDo(verify())
* .andDo(document("resource"));
* }
* </pre> which creates a file "target/snippets/stubs/resource.json" matching any GET
* request to "/resource". To match POST and PUT, you can also specify the content type

View File

@@ -42,20 +42,19 @@ public abstract class WireMockVerifyHelper<T, S extends WireMockVerifyHelper<T,
private MediaType contentType;
private String name;
private MappingBuilder builder;
/**
* @param name the stub name (ignored)
* @return this
* @deprecated in favour of explicitly calling <code>andDo(document(name))</code>
*/
@SuppressWarnings("unchecked")
@Deprecated
public S stub(String name) {
this.name = name;
return (S) this;
}
protected String getName() {
return this.name;
}
public void configure(T result) {
Map<String, Object> configuration = getConfiguration(result);
String actual = new String(getRequestBodyContent(result),

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.contract.wiremock;
import java.io.File;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.Rule;
import org.junit.Test;
@@ -34,12 +36,16 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
@@ -54,14 +60,16 @@ public class WiremockServerRestDocsMatcherApplicationTests {
@Test
public void matchesRequest() throws Exception {
FileSystemUtils.deleteRecursively(new File("target/snippets/stubs/posted.json"));
this.mockMvc
.perform(MockMvcRequestBuilders.post("/resource").content("greeting")
.contentType(MediaType.TEXT_PLAIN))
.andExpect(MockMvcResultMatchers.content().string("Hello World"))
.andDo(WireMockRestDocs.verify()
.wiremock(WireMock.post(WireMock.urlPathEqualTo("/resource"))
.withRequestBody(WireMock.matching("greeting.*")))
.stub("posted"));
.withRequestBody(WireMock.matching("greeting.*"))))
.andDo(document("posted"));
assertThat(new File("target/snippets/stubs/posted.json")).exists();
}
@Test
@@ -74,8 +82,8 @@ public class WiremockServerRestDocsMatcherApplicationTests {
.andExpect(MockMvcResultMatchers.content().string("Hello World"))
.andDo(WireMockRestDocs.verify()
.wiremock(WireMock.post(WireMock.urlPathEqualTo("/resource"))
.withRequestBody(WireMock.matching("garbage.*")))
.stub("posted"));
.withRequestBody(WireMock.matching("garbage.*"))))
.andDo(document("posted"));
}
@Configuration

View File

@@ -77,7 +77,9 @@ public class ContractDslSnippetTests {
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
.apply(documentationConfiguration(this.restDocumentation).snippets()
.withAdditionalDefaults(new WireMockSnippet()))
.build();
}
@Test
@@ -92,13 +94,13 @@ public class ContractDslSnippetTests {
// first WireMock
.andDo(WireMockRestDocs.verify().jsonPath("$[?(@.foo >= 20)]")
.jsonPath("$[?(@.bar in ['baz','bazz','bazzz'])]")
.contentType(MediaType.valueOf("application/json"))
.stub("shouldGrantABeerIfOldEnough"))
.contentType(MediaType.valueOf("application/json")))
// then Contract DSL documentation
.andDo(document("index", SpringCloudContractRestDocs.dslContract()));
// end::contract_snippet[]
then(file("/contracts/index.groovy")).exists();
then(file("/stubs/index.json")).exists();
then(file("/index/dsl-contract.adoc")).exists();
Collection<Contract> parsedContracts = ContractVerifierDslConverter
.convertAsCollection(new File("/"), file("/contracts/index.groovy"));
@@ -131,8 +133,7 @@ public class ContractDslSnippetTests {
// first WireMock
.andDo(WireMockRestDocs.verify().jsonPath("$[?(@.foo >= 20)]")
.jsonPath("$[?(@.bar in ['baz','bazz','bazzz'])]")
.contentType(MediaType.valueOf("application/json"))
.stub("shouldGrantABeerIfOldEnough"))
.contentType(MediaType.valueOf("application/json")))
// then Contract DSL documentation
.andDo(document("{methodName}",
SpringCloudContractRestDocs.dslContract()));
@@ -140,6 +141,9 @@ public class ContractDslSnippetTests {
then(file(
"/contracts/should_create_contract_template_and_doc_with_placeholder_names.groovy"))
.exists();
then(file(
"/stubs/should_create_contract_template_and_doc_with_placeholder_names.json"))
.exists();
then(file(
"/should_create_contract_template_and_doc_with_placeholder_names/dsl-contract.adoc"))
.exists();