Skip to content

Instantly share code, notes, and snippets.

@abhijat
Last active September 18, 2017 16:00
Show Gist options
  • Save abhijat/d38cb27370ffa14b1688810c8f9f70ec to your computer and use it in GitHub Desktop.
Save abhijat/d38cb27370ffa14b1688810c8f9f70ec to your computer and use it in GitHub Desktop.
autoconfigure spring boot rest doc
package am;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.request.ParameterDescriptor;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.hamcrest.CoreMatchers.containsString;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(ServiceController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class DocGenerationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnDefaultMessage() throws Exception {
final MockHttpServletRequestBuilder requestBuilder = get("/")
.accept(MediaType.APPLICATION_JSON);
final FieldDescriptor messageField = fieldWithPath("message").description("A simple message");
final FieldDescriptor languageField = fieldWithPath("language").description("primary message language!");
final Snippet responseFields = responseFields(messageField, languageField);
this.mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello world")))
.andDo(document("index-page", responseFields));
}
@Test
public void generateDocumentationForIdGetRequest() throws Exception {
final MockHttpServletRequestBuilder r = RestDocumentationRequestBuilders.get("/{id}", 111)
.accept(MediaType.APPLICATION_JSON);
final FieldDescriptor nameField = fieldWithPath("name")
.description("name of the person returned");
final ParameterDescriptor idField = parameterWithName("id")
.description("the id of the person to be retrieved");
this.mockMvc.perform(r)
.andExpect(status().isOk())
.andDo(document("person", pathParameters(idField), responseFields(nameField)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment