Skip to content

Commit 1066851

Browse files
committed
feat[post]:Dummytest
1 parent a49dfbe commit 1066851

File tree

7 files changed

+349
-191
lines changed

7 files changed

+349
-191
lines changed

backend/backend/src/main/java/com/ai/lawyer/domain/post/service/PostServiceImpl.java

Lines changed: 0 additions & 1 deletion
This file was deleted.

backend/src/main/java/com/ai/lawyer/domain/post/controller/PostDummyController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ public ResponseEntity<String> deleteDummyMembers() {
3838
int deleted = dummyService.deleteDummyMembers();
3939
return ResponseEntity.ok("더미 멤버 " + deleted + "명 삭제 완료");
4040
}
41-
}
41+
}
Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,79 @@
11
package com.ai.lawyer.domain.poll.controller;
22

33
import com.ai.lawyer.domain.poll.service.PollService;
4-
import org.junit.jupiter.api.Test;
4+
import com.ai.lawyer.domain.poll.dto.PollDto;
5+
import com.ai.lawyer.domain.poll.dto.PollVoteDto;
6+
import com.ai.lawyer.domain.poll.dto.PollStaticsResponseDto;
7+
import com.ai.lawyer.global.jwt.TokenProvider;
8+
import com.ai.lawyer.global.security.SecurityConfig;
9+
import com.ai.lawyer.domain.post.service.PostService;
10+
import com.ai.lawyer.domain.member.repositories.MemberRepository;
11+
import com.ai.lawyer.global.jwt.CookieUtil;
12+
import com.ai.lawyer.global.oauth.CustomOAuth2UserService;
13+
import com.ai.lawyer.global.oauth.OAuth2SuccessHandler;
14+
import com.ai.lawyer.global.oauth.OAuth2FailureHandler;
515
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.DisplayName;
17+
import org.junit.jupiter.api.Test;
618
import org.mockito.Mockito;
719
import org.springframework.beans.factory.annotation.Autowired;
820
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
21+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
22+
import org.springframework.context.annotation.Import;
23+
import org.springframework.http.MediaType;
924
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1025
import org.springframework.test.web.servlet.MockMvc;
11-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13-
import com.ai.lawyer.domain.poll.dto.PollDto;
14-
import com.ai.lawyer.domain.poll.dto.PollVoteDto;
15-
import org.springframework.context.annotation.Import;
16-
import com.ai.lawyer.global.security.SecurityConfig;
17-
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
18-
import org.junit.jupiter.api.DisplayName;
26+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
27+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
28+
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;
29+
import org.springframework.data.redis.core.RedisTemplate;
1930
import jakarta.servlet.http.Cookie;
31+
2032
import static org.mockito.BDDMockito.*;
21-
import com.ai.lawyer.global.jwt.TokenProvider;
22-
import com.ai.lawyer.domain.poll.dto.PollStaticsResponseDto;
33+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
34+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
35+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
36+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
37+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
38+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
39+
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
40+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
41+
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
2342

2443
@Import(SecurityConfig.class)
2544
@AutoConfigureMockMvc
2645
@WebMvcTest(
27-
controllers = PollController.class,
28-
excludeAutoConfiguration = {
29-
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class,
30-
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration.class,
31-
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
32-
org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.class
33-
}
46+
controllers = PollController.class,
47+
excludeAutoConfiguration = {
48+
HibernateJpaAutoConfiguration.class,
49+
JpaRepositoriesAutoConfiguration.class,
50+
DataSourceAutoConfiguration.class,
51+
JpaBaseConfiguration.class
52+
}
3453
)
3554
class PollControllerTest {
3655
@Autowired
3756
private MockMvc mockMvc;
3857
@MockitoBean
3958
private PollService pollService;
4059
@MockitoBean
41-
private com.ai.lawyer.domain.post.service.PostService postService;
60+
private PostService postService;
4261
@MockitoBean
43-
private com.ai.lawyer.global.jwt.TokenProvider tokenProvider;
62+
private TokenProvider tokenProvider;
4463
@MockitoBean
45-
private com.ai.lawyer.global.jwt.CookieUtil cookieUtil;
64+
private CookieUtil cookieUtil;
4665
@MockitoBean
47-
private com.ai.lawyer.domain.member.repositories.MemberRepository memberRepository;
66+
private MemberRepository memberRepository;
4867
@MockitoBean
49-
private org.springframework.data.jpa.mapping.JpaMetamodelMappingContext jpaMappingContext;
68+
private JpaMetamodelMappingContext jpaMappingContext;
5069
@MockitoBean
51-
private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate;
70+
private RedisTemplate<String, Object> redisTemplate;
5271
@MockitoBean
53-
private com.ai.lawyer.global.oauth.CustomOAuth2UserService customOAuth2UserService;
72+
private CustomOAuth2UserService customOAuth2UserService;
5473
@MockitoBean
55-
private com.ai.lawyer.global.oauth.OAuth2SuccessHandler oauth2SuccessHandler;
74+
private OAuth2SuccessHandler oauth2SuccessHandler;
5675
@MockitoBean
57-
private com.ai.lawyer.global.oauth.OAuth2FailureHandler oauth2FailureHandler;
76+
private OAuth2FailureHandler oauth2FailureHandler;
5877

5978
@BeforeEach
6079
void setUp() {
@@ -72,7 +91,7 @@ void t1() throws Exception {
7291
Mockito.when(pollService.getPoll(Mockito.anyLong(), Mockito.anyLong())).thenReturn(null);
7392

7493
mockMvc.perform(get("/api/polls/1")
75-
.cookie(new Cookie("accessToken", "valid-access-token")))
94+
.cookie(new Cookie("accessToken", "valid-access-token")))
7695
.andExpect(status().isOk());
7796
}
7897

@@ -82,7 +101,7 @@ void t2() throws Exception {
82101
Mockito.when(pollService.vote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong())).thenReturn(null);
83102

84103
mockMvc.perform(
85-
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post("/api/polls/1/vote")
104+
post("/api/polls/1/vote")
86105
.param("pollItemsId", "1")
87106
.cookie(new Cookie("accessToken", "valid-access-token"))
88107
).andExpect(status().isOk());
@@ -94,7 +113,7 @@ void t3() throws Exception {
94113
Mockito.when(pollService.getPollStatics(Mockito.anyLong())).thenReturn(new PollStaticsResponseDto());
95114

96115
mockMvc.perform(get("/api/polls/1/statics")
97-
.cookie(new Cookie("accessToken", "valid-access-token")))
116+
.cookie(new Cookie("accessToken", "valid-access-token")))
98117
.andExpect(status().isOk());
99118
}
100119

@@ -104,7 +123,7 @@ void t4() throws Exception {
104123
Mockito.doNothing().when(pollService).closePoll(Mockito.anyLong());
105124

106125
mockMvc.perform(
107-
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put("/api/polls/1/close")
126+
put("/api/polls/1/close")
108127
.cookie(new Cookie("accessToken", "valid-access-token"))
109128
).andExpect(status().isOk());
110129
}
@@ -117,7 +136,7 @@ void t5() throws Exception {
117136
Mockito.doNothing().when(pollService).deletePoll(Mockito.anyLong(), Mockito.anyLong());
118137

119138
mockMvc.perform(
120-
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete("/api/polls/1")
139+
delete("/api/polls/1")
121140
.cookie(new Cookie("accessToken", "valid-access-token"))
122141
).andExpect(status().isOk());
123142
}
@@ -128,7 +147,7 @@ void t6() throws Exception {
128147
Mockito.when(pollService.getTopPollByStatus(Mockito.any(), Mockito.anyLong())).thenReturn(null);
129148

130149
mockMvc.perform(get("/api/polls/top/ongoing")
131-
.cookie(new Cookie("accessToken", "valid-access-token")))
150+
.cookie(new Cookie("accessToken", "valid-access-token")))
132151
.andExpect(status().isOk());
133152
}
134153

@@ -138,7 +157,7 @@ void t7() throws Exception {
138157
Mockito.when(pollService.getTopPollByStatus(Mockito.any(), Mockito.anyLong())).thenReturn(null);
139158

140159
mockMvc.perform(get("/api/polls/top/closed")
141-
.cookie(new Cookie("accessToken", "valid-access-token")))
160+
.cookie(new Cookie("accessToken", "valid-access-token")))
142161
.andExpect(status().isOk());
143162
}
144163

@@ -148,8 +167,8 @@ void t8() throws Exception {
148167
Mockito.when(pollService.createPoll(Mockito.any(), Mockito.anyLong())).thenReturn(null);
149168

150169
mockMvc.perform(
151-
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post("/api/polls")
152-
.contentType(org.springframework.http.MediaType.APPLICATION_JSON)
170+
post("/api/polls")
171+
.contentType(MediaType.APPLICATION_JSON)
153172
.content("{}")
154173
.cookie(new Cookie("accessToken", "valid-access-token"))
155174
).andExpect(status().isOk());
@@ -162,10 +181,10 @@ void t9() throws Exception {
162181
Mockito.when(pollService.getPoll(Mockito.anyLong(), Mockito.anyLong())).thenReturn(responseDto);
163182

164183
mockMvc.perform(get("/api/polls/1")
165-
.cookie(new Cookie("accessToken", "valid-access-token")))
184+
.cookie(new Cookie("accessToken", "valid-access-token")))
166185
.andExpect(status().isOk())
167-
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.result.pollId").value(1L))
168-
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.result.voteTitle").value("테스트 투표"));
186+
.andExpect(MockMvcResultMatchers.jsonPath("$.result.pollId").value(1L))
187+
.andExpect(MockMvcResultMatchers.jsonPath("$.result.voteTitle").value("테스트 투표"));
169188
}
170189

171190
@Test
@@ -175,11 +194,11 @@ void t10() throws Exception {
175194
Mockito.when(pollService.vote(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong())).thenReturn(responseDto);
176195

177196
mockMvc.perform(
178-
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post("/api/polls/1/vote")
179-
.param("pollItemsId", "1")
180-
.cookie(new Cookie("accessToken", "valid-access-token"))
181-
).andExpect(status().isOk())
182-
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.result.pollId").value(1L))
183-
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.result.memberId").value(1L));
197+
post("/api/polls/1/vote")
198+
.param("pollItemsId", "1")
199+
.cookie(new Cookie("accessToken", "valid-access-token"))
200+
).andExpect(status().isOk())
201+
.andExpect(MockMvcResultMatchers.jsonPath("$.result.pollId").value(1L))
202+
.andExpect(MockMvcResultMatchers.jsonPath("$.result.memberId").value(1L));
184203
}
185204
}

backend/src/test/java/com/ai/lawyer/domain/poll/service/PollServiceTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import org.mockito.Mockito;
1313
import org.mockito.MockitoAnnotations;
1414
import org.springframework.web.server.ResponseStatusException;
15+
import java.util.List;
1516
import java.util.Collections;
17+
import org.springframework.http.HttpStatus;
1618
import static org.assertj.core.api.Assertions.assertThat;
1719
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1820

@@ -37,9 +39,9 @@ void t1() {
3739
@Test
3840
@DisplayName("투표 옵션 목록 조회")
3941
void t2() {
40-
java.util.List expected = java.util.Collections.emptyList();
42+
List expected = Collections.emptyList();
4143
Mockito.when(pollService.getPollOptions(Mockito.anyLong())).thenReturn(expected);
42-
java.util.List result = pollService.getPollOptions(1L);
44+
List result = pollService.getPollOptions(1L);
4345
assertThat(result).isEqualTo(expected);
4446
}
4547

@@ -143,18 +145,18 @@ void t13() {
143145
@Test
144146
@DisplayName("상태별 투표 목록 조회")
145147
void t14() {
146-
java.util.List expected = java.util.Collections.emptyList();
148+
List expected = Collections.emptyList();
147149
Mockito.when(pollService.getPollsByStatus(Mockito.any(), Mockito.anyLong())).thenReturn(expected);
148-
java.util.List result = pollService.getPollsByStatus(PollDto.PollStatus.ONGOING, 1L);
150+
List result = pollService.getPollsByStatus(PollDto.PollStatus.ONGOING, 1L);
149151
assertThat(result).isEqualTo(expected);
150152
}
151153

152154
@Test
153155
@DisplayName("상태별 Top N 투표 목록 조회")
154156
void t15() {
155-
java.util.List expected = java.util.Collections.emptyList();
157+
List expected = Collections.emptyList();
156158
Mockito.when(pollService.getTopNPollsByStatus(Mockito.any(), Mockito.anyInt(), Mockito.anyLong())).thenReturn(expected);
157-
java.util.List result = pollService.getTopNPollsByStatus(PollDto.PollStatus.ONGOING, 3, 1L);
159+
List result = pollService.getTopNPollsByStatus(PollDto.PollStatus.ONGOING, 3, 1L);
158160
assertThat(result).isEqualTo(expected);
159161
}
160162

@@ -163,7 +165,7 @@ void t15() {
163165
void t16() {
164166
PollCreateDto dto = new PollCreateDto();
165167
dto.setVoteTitle("테스트 투표");
166-
Mockito.doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.BAD_REQUEST, "게시글 ID(postId)는 필수입니다.")).when(pollService).createPoll(Mockito.any(), Mockito.anyLong());
168+
Mockito.doThrow(new ResponseStatusException(HttpStatus.BAD_REQUEST, "게시글 ID(postId)는 필수입니다.")).when(pollService).createPoll(Mockito.any(), Mockito.anyLong());
167169
assertThatThrownBy(() -> pollService.createPoll(dto, 1L))
168170
.isInstanceOf(ResponseStatusException.class)
169171
.hasMessageContaining("게시글 ID(postId)는 필수입니다.");
@@ -174,7 +176,7 @@ void t16() {
174176
void t17() {
175177
PollCreateDto dto = new PollCreateDto();
176178
dto.setPostId(1L);
177-
Mockito.doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.BAD_REQUEST, "투표 제목(voteTitle)은 필수입니다.")).when(pollService).createPoll(Mockito.any(), Mockito.anyLong());
179+
Mockito.doThrow(new ResponseStatusException(HttpStatus.BAD_REQUEST, "투표 제목(voteTitle)은 필수입니다.")).when(pollService).createPoll(Mockito.any(), Mockito.anyLong());
178180
assertThatThrownBy(() -> pollService.createPoll(dto, 1L))
179181
.isInstanceOf(ResponseStatusException.class)
180182
.hasMessageContaining("투표 제목(voteTitle)은 필수입니다.");

0 commit comments

Comments
 (0)