1+ package com .example ;
2+
3+ import org .junit .Before ;
4+ import org .junit .Rule ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .boot .test .SpringApplicationConfiguration ;
9+ import org .springframework .restdocs .RestDocumentation ;
10+ import org .springframework .restdocs .mockmvc .RestDocumentationResultHandler ;
11+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
12+ import org .springframework .test .context .web .WebAppConfiguration ;
13+ import org .springframework .test .web .servlet .MockMvc ;
14+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
15+ import org .springframework .web .context .WebApplicationContext ;
16+
17+ import static org .springframework .restdocs .hypermedia .HypermediaDocumentation .linkWithRel ;
18+ import static org .springframework .restdocs .hypermedia .HypermediaDocumentation .links ;
19+ import static org .springframework .restdocs .mockmvc .MockMvcRestDocumentation .document ;
20+ import static org .springframework .restdocs .mockmvc .MockMvcRestDocumentation .documentationConfiguration ;
21+ import static org .springframework .restdocs .mockmvc .RestDocumentationRequestBuilders .get ;
22+ import static org .springframework .restdocs .operation .preprocess .Preprocessors .*;
23+ import static org .springframework .restdocs .payload .PayloadDocumentation .fieldWithPath ;
24+ import static org .springframework .restdocs .payload .PayloadDocumentation .responseFields ;
25+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
26+
27+ @ RunWith (SpringJUnit4ClassRunner .class )
28+ @ SpringApplicationConfiguration (classes = SpringRestDocsApplication .class )
29+ @ WebAppConfiguration
30+ public class ApiDocumentation {
31+
32+ @ Rule
33+ public final RestDocumentation restDocumentation = new RestDocumentation ("target/generated-snippets" );
34+
35+ @ Autowired
36+ private WebApplicationContext context ;
37+
38+ private RestDocumentationResultHandler document ;
39+
40+ private MockMvc mockMvc ;
41+
42+ @ Before
43+ public void setUp () {
44+ this .document = document ("{method-name}" , preprocessRequest (prettyPrint ()), preprocessResponse (prettyPrint ()));
45+
46+ this .mockMvc = MockMvcBuilders .webAppContextSetup (this .context )
47+ .apply (documentationConfiguration (this .restDocumentation ))
48+ .alwaysDo (this .document ).build ();
49+ }
50+
51+ @ Test
52+ public void indexExample () throws Exception {
53+ this .document .snippets (
54+ links (
55+ linkWithRel ("notes" ).description ("The <<Simple description about the REST Service >>" ),
56+ linkWithRel ("tags" ).description ("The <<resources-tags,Tags resource>>" )
57+ ),
58+ responseFields (fieldWithPath ("_links" ).description ("<<resources-index-links,Links>> to other resources" )));
59+
60+ this .mockMvc .perform (get ("/api" )).andExpect (status ().isOk ());
61+ }
62+
63+ @ Test
64+ public void contextLoads () {
65+ }
66+ }
0 commit comments