|
17 | 17 |
|
18 | 18 | import static org.hamcrest.CoreMatchers.*;
|
19 | 19 | import static org.springframework.http.HttpHeaders.*;
|
20 |
| -import static org.springframework.restdocs.RestDocumentation.*; |
| 20 | +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*; |
21 | 21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
22 | 22 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
23 | 23 |
|
24 | 24 | import org.junit.jupiter.api.BeforeEach;
|
25 | 25 | import org.junit.jupiter.api.Test;
|
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
26 | 27 |
|
27 | 28 | import org.springframework.beans.factory.annotation.Autowired;
|
28 | 29 | import org.springframework.boot.test.context.SpringBootTest;
|
29 |
| -import org.springframework.restdocs.config.RestDocumentationConfigurer; |
| 30 | +import org.springframework.restdocs.JUnitRestDocumentation; |
| 31 | +import org.springframework.restdocs.RestDocumentationContextProvider; |
| 32 | +import org.springframework.restdocs.RestDocumentationExtension; |
| 33 | +import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; |
30 | 34 | import org.springframework.test.web.servlet.MockMvc;
|
31 | 35 | import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
32 | 36 | import org.springframework.web.context.WebApplicationContext;
|
|
37 | 41 | * @soundtrack The Intersphere - Out of phase (Live at Alte Feuerwache Mannheim)
|
38 | 42 | */
|
39 | 43 | @SpringBootTest
|
| 44 | +@ExtendWith(RestDocumentationExtension.class) |
40 | 45 | public class WebIntegrationTests {
|
41 | 46 |
|
42 |
| - @Autowired WebApplicationContext context; |
43 |
| - @Autowired CustomerRepository customers; |
| 47 | + public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); |
44 | 48 |
|
45 |
| - private MockMvc mvc; |
| 49 | + @Autowired |
| 50 | + WebApplicationContext context; |
| 51 | + @Autowired |
| 52 | + CustomerRepository customers; |
46 | 53 |
|
47 |
| - @BeforeEach |
48 |
| - public void setUp() { |
| 54 | + private MockMvc mvc; |
49 | 55 |
|
50 |
| - this.mvc = MockMvcBuilders.webAppContextSetup(context).// |
51 |
| - apply(new RestDocumentationConfigurer()).// |
52 |
| - build(); |
53 |
| - } |
| 56 | + @BeforeEach |
| 57 | + public void setUp(RestDocumentationContextProvider restDocumentation) { |
54 | 58 |
|
55 |
| - @Test |
56 |
| - public void executeConditionalGetRequests() throws Exception { |
| 59 | + this.mvc = MockMvcBuilders.webAppContextSetup(context).// |
| 60 | + apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)).// |
| 61 | + build(); |
| 62 | + } |
57 | 63 |
|
58 |
| - var customer = customers.findAll().iterator().next(); |
59 |
| - var uri = new UriTemplate("/customers/{id}").expand(customer.getId()); |
| 64 | + @Test |
| 65 | + public void executeConditionalGetRequests() throws Exception { |
60 | 66 |
|
61 |
| - var response = mvc.perform(get(uri)).// |
62 |
| - andExpect(header().string(ETAG, is(notNullValue()))).// |
63 |
| - andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
64 |
| - andReturn().getResponse(); |
| 67 | + var customer = customers.findAll().iterator().next(); |
| 68 | + var uri = new UriTemplate("/customers/{id}").expand(customer.getId()); |
65 | 69 |
|
66 |
| - // ETag-based |
| 70 | + var response = mvc.perform(get(uri)).// |
| 71 | + andExpect(header().string(ETAG, is(notNullValue()))).// |
| 72 | + andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
| 73 | + andReturn().getResponse(); |
67 | 74 |
|
68 |
| - response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).// |
69 |
| - andExpect(status().isNotModified()).// |
70 |
| - andExpect(header().string(ETAG, is(notNullValue()))).// |
71 |
| - andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
72 |
| - andDo(document("if-none-match")).// |
73 |
| - andReturn().getResponse(); |
| 75 | + // ETag-based |
74 | 76 |
|
75 |
| - // Last-modified-based |
| 77 | + response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).// |
| 78 | + andExpect(status().isNotModified()).// |
| 79 | + andExpect(header().string(ETAG, is(notNullValue()))).// |
| 80 | + andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
| 81 | + andDo(document("if-none-match")).// |
| 82 | + andReturn().getResponse(); |
76 | 83 |
|
77 |
| - mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).// |
78 |
| - andExpect(status().isNotModified()).// |
79 |
| - andExpect(header().string(ETAG, is(notNullValue()))).// |
80 |
| - andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
81 |
| - andDo(document("if-modified-since")); |
82 |
| - } |
| 84 | + // Last-modified-based |
| 85 | + |
| 86 | + mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).// |
| 87 | + andExpect(status().isNotModified()).// |
| 88 | + andExpect(header().string(ETAG, is(notNullValue()))).// |
| 89 | + andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
| 90 | + andDo(document("if-modified-since")); |
| 91 | + } |
83 | 92 | }
|
0 commit comments