1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package cn.idea360.docs;
import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.RestDocumentationExtension; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;
import static org.mockito.internal.verification.VerificationModeFactory.description; import static org.springframework.restdocs.cli.CliDocumentation.curlRequest; import static org.springframework.restdocs.http.HttpDocumentation.httpRequest; import static org.springframework.restdocs.http.HttpDocumentation.httpResponse; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields; import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class}) @SpringBootTest @AutoConfigureRestDocs(outputDir = "target/generated-snippets") class SpringRestDocsDemoApplicationTests {
@Autowired private WebApplicationContext context;
private MockMvc mockMvc;
List<Order> orders=null;
@BeforeEach public void setUp(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) {
this.mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(documentationConfiguration(restDocumentation)) .build();
orders= Stream.of(new Order(101, "Mobile", 1, 5700) ,new Order(102, "notebook", 1, 18488)) .collect(Collectors.toList()); }
@Test public void testAddOrder() throws Exception { String ordersJson=new ObjectMapper().writeValueAsString(orders); mockMvc.perform(post("/placeOrder") .content(ordersJson) .contentType("application/json")).andDo(print()) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.content().json(ordersJson)) .andDo(document("{methodName}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()))); }
@Test public void testGetOrders() throws Exception { mockMvc.perform(get("/getOrders") .contentType("application/json")).andDo(print()) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.content().json(new ObjectMapper().writeValueAsString(orders))) .andDo(document("{methodName}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()))); }
}
|