diff --git a/apps/h2db/crm b/apps/h2db/crm new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/web/chat/backend/entity/Room.java b/src/main/java/web/chat/backend/entity/Room.java index 3c3057a..96b04bd 100644 --- a/src/main/java/web/chat/backend/entity/Room.java +++ b/src/main/java/web/chat/backend/entity/Room.java @@ -16,6 +16,7 @@ @NoArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) +@Builder @Getter @Setter @Entity diff --git a/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java b/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java index 6ea93d5..1dc72cd 100644 --- a/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java +++ b/src/test/java/web/chat/backend/controller/RoomControllerIntegrationTest.java @@ -78,8 +78,11 @@ void shouldRespond400StatusCode_ifNotFoundRoom() throws Exception { void getRooms() throws Exception { // when + ResultActions resultActions = mockMvc.perform(get("/api/rooms")); // then + resultActions.andExpect(status().isOk()) + .andDo(print()); } @Test diff --git a/src/test/java/web/chat/backend/controller/RoomControllerTest.java b/src/test/java/web/chat/backend/controller/RoomControllerTest.java index d6201c8..ee22025 100644 --- a/src/test/java/web/chat/backend/controller/RoomControllerTest.java +++ b/src/test/java/web/chat/backend/controller/RoomControllerTest.java @@ -2,11 +2,17 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.util.Arrays; +import java.util.List; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -22,6 +28,7 @@ import lombok.RequiredArgsConstructor; import web.chat.backend.controller.request.RoomRequest; import web.chat.backend.controller.response.RoomResponse; + import web.chat.backend.entity.Room; import web.chat.backend.service.MessageService; import web.chat.backend.service.RoomService; @@ -44,10 +51,33 @@ class RoomControllerTest { void getRooms() throws Exception { // given + Room r1 = Room.builder() + .id(1L) + .title("test1") + .build(); + Room r2 = Room.builder() + .id(2L) + .title("test2") + .build(); + Room r3 = Room.builder() + .id(3L) + .title("test3") + .build(); + List roomList = Arrays.asList(r1, r2, r3); + + given(roomService.getRooms()).willReturn(roomList); // when + ResultActions resultActions = mockMvc.perform(get("/api/rooms")); // then + resultActions.andExpect(status().isOk()) + .andExpect(jsonPath("$.rooms", not(empty()))) + .andExpect(jsonPath("$.rooms", hasSize(3))) + .andExpect(jsonPath("$.rooms[0].id", is(1))) + .andExpect(jsonPath("$.rooms[1].id", is(2))) + .andExpect(jsonPath("$.rooms[2].id", is(3))) + .andDo(print()); } @Test @@ -105,9 +135,21 @@ void createRoom_titleLengthLessThan_2() throws Exception { void createRoom_titleLengthExceed_20() throws Exception { // given + RoomRequest req = new RoomRequest(); + req.setTitle("title length exceeds 20"); + + final String body = objectMapper.writeValueAsString(req); // when + ResultActions resultActions = mockMvc.perform( + post("/api/rooms") + .contentType(MediaType.APPLICATION_JSON) + .content(body)); // then + resultActions.andExpect(status().is4xxClientError()) + .andExpect(result -> assertTrue(result.getResolvedException() instanceof MethodArgumentNotValidException)) + .andDo(print()); + } }