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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| @ExtendWith({RestDocumentationExtension.class, SpringExtension.class}) @WebMvcTest(UserController.class) @AutoConfigureRestDocs class UserControllerTest { @Autowired private MockMvc mockMvc; @MockBean private UserService userService; @Autowired private ObjectMapper objectMapper;
@Test void findUser() throws Exception { given(userService.findUserByUserSeq(1L)).willReturn(User.of(1L, "thinkub", "Ming"));
ResultActions resultActions = mockMvc.perform(RestDocumentationRequestBuilders.get("/user/{userSeq}", 1L) .accept(MediaType.APPLICATION_JSON_VALUE)) .andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().isOk()) .andDo(document( "findUser", getDocumentRequest(), getDocumentResponse(), pathParameters(parameterWithName("userSeq").description("사용자 시퀀스")), responseFields( fieldWithPath("userSeq").type(JsonFieldType.NUMBER).description("사용자 시퀀스"), fieldWithPath("userId").type(JsonFieldType.STRING).description("아이디"), fieldWithPath("userName").type(JsonFieldType.STRING).description("이름") ) )); }
@Test void createUser() throws Exception { User.Create create = User.Create.of("thinkub", "Ming"); given(userService.createUser(any())).willReturn(User.of(1L, "thinkub", "Ming"));
ResultActions resultActions = mockMvc.perform(RestDocumentationRequestBuilders.post("/user") .accept(MediaType.APPLICATION_JSON_VALUE) .contentType(MediaType.APPLICATION_JSON_VALUE) .content(objectMapper.writeValueAsString(create))) .andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().isOk()) .andDo(document( "createUser", getDocumentRequest(), getDocumentResponse(), requestFields( fieldWithPath("userId").type(JsonFieldType.STRING).description("아이디"), fieldWithPath("userName").type(JsonFieldType.STRING).description("이름").optional() ), responseFields( fieldWithPath("userSeq").type(JsonFieldType.NUMBER).description("사용자 시퀀스"), fieldWithPath("userId").type(JsonFieldType.STRING).description("아이디"), fieldWithPath("userName").type(JsonFieldType.STRING).description("이름") ) )); }
@Test void modifyUser() throws Exception { User.Modify modify = User.Modify.of("thinkub-new", "Ming-new"); User user = User.of(1L, "thinkub-new", "Ming-new"); given(userService.modifyUser(eq(1L), any())).willReturn(user);
ResultActions resultActions = mockMvc.perform(RestDocumentationRequestBuilders.put("/user/{userSeq}", 1L) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(modify))) .andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().isOk()) .andDo(document( "modifyUser", getDocumentRequest(), getDocumentResponse(), pathParameters(parameterWithName("userSeq").description("사용자 시퀀스")), requestFields( fieldWithPath("userId").type(JsonFieldType.STRING).description("아이디").optional(), fieldWithPath("userName").type(JsonFieldType.STRING).description("이름").optional() ), responseFields( fieldWithPath("userSeq").type(JsonFieldType.NUMBER).description("사용자 시퀀스"), fieldWithPath("userId").type(JsonFieldType.STRING).description("아이디"), fieldWithPath("userName").type(JsonFieldType.STRING).description("이름") ) ));
}
@Test void deleteUser() throws Exception { doNothing().when(userService).deleteUser(1L);
ResultActions resultActions = mockMvc.perform(RestDocumentationRequestBuilders.delete("/user/{userSeq}", 1L) .accept(MediaType.APPLICATION_JSON_VALUE)) .andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().isOk()) .andDo(document( "deleteUser", getDocumentRequest(), getDocumentResponse(), pathParameters(parameterWithName("userSeq").description("사용자 시퀀스")) )); } }
|