Add support for restassured with restdocs (#431)

fixes gh-334
This commit is contained in:
Eddú Meléndez Gonzales
2017-10-16 03:02:40 -05:00
committed by Marcin Grzejszczak
parent 0785131069
commit 4a9fb48056
4 changed files with 126 additions and 1 deletions

View File

@@ -17,6 +17,16 @@
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
@@ -39,6 +49,11 @@
<artifactId>spring-restdocs-mockmvc</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-restassured</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -0,0 +1,26 @@
package org.springframework.cloud.contract.wiremock.restdocs;
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsRestAssuredConfigurationCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
/**
* Custom configuration for Spring RestDocs that adds a WireMock snippet (for generating
* JSON stubs). Applied automatically if you use
* {@link org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs @AutoConfigureRestDocs} in your test case and this class
* is available. JSON stubs are generated and added to the restdocs path under "stubs".
*
* @see WireMockRestDocs for a convenient entry point for customizing and asserting the
* stub behaviour
*
* @author Eddú Meléndez
*/
@Configuration
public class WireMockRestAssuredConfiguration implements RestDocsRestAssuredConfigurationCustomizer {
@Override
public void customize(RestAssuredRestDocumentationConfigurer configurer) {
configurer.snippets().withDefaults(new WireMockSnippet());
}
}

View File

@@ -4,4 +4,5 @@ org.springframework.cloud.contract.wiremock.WireMockApplicationListener
# RestDocs Auto Configuration
org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs=\
org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocsConfiguration
org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocsConfiguration,\
org.springframework.cloud.contract.wiremock.restdocs.WireMockRestAssuredConfiguration

View File

@@ -0,0 +1,83 @@
package org.springframework.cloud.contract.wiremock;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;
import org.junit.runner.RunWith;
import wiremock.org.eclipse.jetty.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ServletWebServerFactoryAutoConfiguration.EmbeddedTomcat.class, DispatcherServletAutoConfiguration.class},
webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class WiremockServerRestAssuredApplicationTests {
@Autowired
private RequestSpecification documentationSpec;
@LocalServerPort
private int port;
@Test
public void contextLoads() throws Exception {
given()
.port(this.port)
.when()
.get("/resource")
.then()
.assertThat()
.statusCode(is(200))
.content(equalTo("Hello World"));
}
@Test
public void statusIsMaintained() throws Exception {
given(this.documentationSpec)
.filter(document("status"))
.when()
.get("/status")
.then()
.assertThat()
.statusCode(is(202))
.content(equalTo("Hello World"));
}
@Configuration
@EnableWebMvc
@RestController
protected static class TestConfiguration {
@ResponseBody
@RequestMapping("/resource")
public String resource() {
return "Hello World";
}
@ResponseBody
@RequestMapping("/status")
public ResponseEntity<String> status() {
return ResponseEntity.status(HttpStatus.ACCEPTED_202).body("Hello World");
}
}
}