-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
메시지 전송 api 통합 테스트 메서드를 RoomControllerIntegrationTest
처리할 수 있을지 고민
#25
Labels
enhancement
New feature or request
Comments
방법 1
@Test
@Sql(scripts = "/test-sql/messages.sql")
@Transactional
void createMessage() throws Exception {
// given
MessageRequest req = new MessageRequest();
req.setContents("hi hello");
final String body = objectMapper.writeValueAsString(req);
// when
ResultActions action = mockMvc.perform(
post("/api/rooms/{roomId}/messages", 1)
.contentType(MediaType.APPLICATION_JSON)
.content(body));
// then
action.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.contents").value(req.getContents()));
}
@Test
@Sql(scripts = "/test-sql/messages.sql")
@DisplayName("ID가 1인 채팅방의 메시지 리스트가 조회되야만 한다")
@Transactional
void shouldGetMessageList_inRoomIdWith1() throws Exception {
mockMvc.perform(get("/api/rooms/{id}/messages", 1))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.*", not(empty())))
.andExpect(jsonPath("$.*", hasSize(1)))
.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].contents", is("foo")))
.andExpect(jsonPath("$[0].messageType", is("TEXT")));
}
방법 2
clean-up-messages.sqldelete from message; @Test
@Sql(scripts = "/test-sql/messages.sql")
@Sql(scripts = "/test-sql/clean-up-messages.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
void createMessage() throws Exception {
// given
MessageRequest req = new MessageRequest();
req.setContents("hi hello");
final String body = objectMapper.writeValueAsString(req);
// when
ResultActions action = mockMvc.perform(
post("/api/rooms/{roomId}/messages", 1)
.contentType(MediaType.APPLICATION_JSON)
.content(body));
// then
action.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.contents").value(req.getContents()));
}
@Test
@Sql(scripts = "/test-sql/messages.sql")
@Sql(scripts = "/test-sql/clean-up-messages.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
@DisplayName("ID가 1인 채팅방의 메시지 리스트가 조회되야만 한다")
void shouldGetMessageList_inRoomIdWith1() throws Exception {
mockMvc.perform(get("/api/rooms/{id}/messages", 1))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.*", not(empty())))
.andExpect(jsonPath("$.*", hasSize(1)))
.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].contents", is("foo")))
.andExpect(jsonPath("$[0].messageType", is("TEXT")));
}
방법 3
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@RequiredArgsConstructor
@SpringBootTest()
@Transactional
@AutoConfigureTestEntityManager
class RoomControllerIntegrationTest {
// ...
final TestEntityManager entityManager;
@Test
void createMessage() throws Exception {
// given
Room room = new Room();
room.setTitle("foo");
Room savedRoom = entityManager.persist(room);
MessageRequest req = new MessageRequest();
req.setContents("hi hello");
final String body = objectMapper.writeValueAsString(req);
// when
ResultActions action = mockMvc.perform(
post("/api/rooms/{roomId}/messages", savedRoom.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(body));
// then
action.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.contents").value(req.getContents()));
}
@Test
@DisplayName("ID가 1인 채팅방의 메시지 리스트가 조회되야만 한다")
void shouldGetMessageList_inRoomIdWith1() throws Exception {
// given
Room fooRoom = new Room();
fooRoom.setTitle("foo");
Room savedFooRoom = entityManager.persist(fooRoom);
Room barRoom = new Room();
barRoom.setTitle("bar");
entityManager.persist(barRoom);
List<Message> messages = Stream.of(1, 2, 3)
.map((id) -> {
Message message = new Message();
message.setContents("contents_" + id);
message.setMessageType(MessageType.TEXT);
message.setRoom(id % 2 == 0 ? savedFooRoom : barRoom);
return entityManager.persist(message);
}).collect(Collectors.toList());
// when, then
mockMvc.perform(get("/api/rooms/{id}/messages", fooRoom.getId()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.*", not(empty())))
.andExpect(jsonPath("$.*", hasSize(1)))
.andExpect(jsonPath("$[0].id").value(messages.get(1).getId()))
.andExpect(jsonPath("$[0].contents").value(messages.get(1).getContents()))
.andExpect(jsonPath("$[0].messageType").value(messages.get(1).getMessageType().name()));
}
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
참고 링크 #24 (comment)
이 메서드를
MessageCreationTest
에 추가한 이유이 테스트를
RoomControllerIntegrationTest
에 추가할 때RoomControllerIntegrationTest#shouldGetMessageList_inRoomIdWith1
가 영향 받음하나의 클래스에서 처리할 수 없을지 고민 필요
response
error log
The text was updated successfully, but these errors were encountered: