Skip to content

Commit e014de8

Browse files
committed
test: SettlementController 테스트 코드 작성
1 parent deeddb0 commit e014de8

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/test/java/org/c4marathon/assignment/ControllerTestSupport.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
import org.c4marathon.assignment.account.service.SavingAccountService;
88
import org.c4marathon.assignment.member.presentation.MemberController;
99
import org.c4marathon.assignment.member.service.MemberService;
10+
import org.c4marathon.assignment.settlement.presentation.SettlementController;
11+
import org.c4marathon.assignment.settlement.service.SettlementService;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
1214
import org.springframework.boot.test.mock.mockito.MockBean;
1315
import org.springframework.mock.web.MockHttpSession;
1416
import org.springframework.test.web.servlet.MockMvc;
1517

1618
@WebMvcTest(controllers = {
17-
MemberController.class,
18-
AccountController.class,
19-
SavingAccountController.class
19+
MemberController.class,
20+
AccountController.class,
21+
SavingAccountController.class,
22+
SettlementController.class
2023
})
2124
public class ControllerTestSupport {
2225
@Autowired
@@ -34,6 +37,9 @@ public class ControllerTestSupport {
3437
@MockBean
3538
protected SavingAccountService savingAccountService;
3639

40+
@MockBean
41+
protected SettlementService settlementService;
42+
3743
protected MockHttpSession session;
3844

3945
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.c4marathon.assignment.settlement.presentation;
2+
3+
import static org.c4marathon.assignment.settlement.domain.SettlementType.*;
4+
import static org.mockito.Mockito.*;
5+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
6+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
7+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
8+
9+
import java.util.List;
10+
11+
import org.c4marathon.assignment.ControllerTestSupport;
12+
import org.c4marathon.assignment.global.session.SessionConst;
13+
import org.c4marathon.assignment.global.session.SessionMemberInfo;
14+
import org.c4marathon.assignment.settlement.dto.ReceivedSettlementResponse;
15+
import org.c4marathon.assignment.settlement.dto.SettlementDetailInfo;
16+
import org.c4marathon.assignment.settlement.dto.SettlementRequest;
17+
import org.c4marathon.assignment.settlement.dto.SettlementResponse;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.DisplayName;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.http.MediaType;
22+
import org.springframework.mock.web.MockHttpSession;
23+
24+
25+
class SettlementControllerTest extends ControllerTestSupport {
26+
27+
@BeforeEach
28+
void initSession() {
29+
session = new MockHttpSession();
30+
SessionMemberInfo memberInfo = new SessionMemberInfo(1L, "[email protected]", 1L);
31+
session.setAttribute(SessionConst.LOGIN_MEMBER, memberInfo);
32+
}
33+
34+
@Test
35+
@DisplayName("정산 요청 API가 정상적으로 호출된다")
36+
void settle() throws Exception {
37+
// given
38+
SettlementRequest request = new SettlementRequest(3, 30000, List.of(2L, 3L, 4L), EQUAL);
39+
40+
// when & then
41+
mockMvc.perform(post("/api/settle")
42+
.contentType(MediaType.APPLICATION_JSON)
43+
.content(objectMapper.writeValueAsString(request))
44+
.session(session)
45+
)
46+
.andExpect(status().isOk());
47+
}
48+
49+
@DisplayName("내가 요청한 정산 리스트를 정상적으로 조회한다")
50+
@Test
51+
void getRequestedSettlements() throws Exception {
52+
// given
53+
List<SettlementResponse> responses = List.of(
54+
new SettlementResponse(1L, 1L, 30000, List.of(
55+
new SettlementDetailInfo(10L, 2L, 10000),
56+
new SettlementDetailInfo(11L, 3L, 10000),
57+
new SettlementDetailInfo(12L, 4L, 10000)
58+
))
59+
);
60+
61+
when(settlementService.getRequestedSettlements(1L)).thenReturn(responses);
62+
63+
// when & then
64+
mockMvc.perform(
65+
get("/api/settlements/requested")
66+
.session(session)
67+
)
68+
.andDo(print())
69+
.andExpect(status().isOk())
70+
.andExpect(jsonPath("$.size()").value(1))
71+
.andExpect(jsonPath("$[0].settlementId").value(1L))
72+
.andExpect(jsonPath("$[0].requestAccountId").value(1L))
73+
.andExpect(jsonPath("$[0].totalAmount").value(30000));
74+
}
75+
76+
@DisplayName("내가 요청받은 정산 리스트를 정장석으로 조회한다.")
77+
@Test
78+
void getReceivedSettlements() throws Exception {
79+
// given
80+
List<ReceivedSettlementResponse> responses = List.of(
81+
new ReceivedSettlementResponse(1L, 2L, 30000, 1L, 10000)
82+
);
83+
84+
when(settlementService.getReceivedSettlements(1L)).thenReturn(responses);
85+
// when // then
86+
mockMvc.perform(
87+
get("/api/settlements/received")
88+
.session(session)
89+
)
90+
.andDo(print())
91+
.andExpect(status().isOk())
92+
.andExpect(jsonPath("$.size()").value(1))
93+
.andExpect(jsonPath("$[0].settlementId").value(1L))
94+
.andExpect(jsonPath("$[0].requestAccountId").value(2L))
95+
.andExpect(jsonPath("$[0].totalAmount").value(30000))
96+
.andExpect(jsonPath("$[0].myAccountId").value(1L))
97+
.andExpect(jsonPath("$[0].mySettlementAmount").value(10000));
98+
}
99+
}

0 commit comments

Comments
 (0)