Skip to content

Commit 4a29327

Browse files
committed
test: 솜커톤 참가자 생성, 조회, 삭제 기능 테스트
- SomParticipantServiceTest (테스트)
1 parent ab66613 commit 4a29327

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package dmu.dasom.api.domain.somkathon;
2+
3+
import dmu.dasom.api.domain.common.exception.CustomException;
4+
import dmu.dasom.api.domain.common.exception.ErrorCode;
5+
import dmu.dasom.api.domain.somkathon.dto.SomParticipantRequestDto;
6+
import dmu.dasom.api.domain.somkathon.dto.SomParticipantResponseDto;
7+
import dmu.dasom.api.domain.somkathon.entity.SomParticipant;
8+
import dmu.dasom.api.domain.somkathon.repository.SomParticipantRepository;
9+
import dmu.dasom.api.domain.somkathon.service.SomParticipantService;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.ExtendWith;
13+
import org.mockito.InjectMocks;
14+
import org.mockito.Mock;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
17+
import java.util.List;
18+
import java.util.Optional;
19+
20+
import static org.junit.jupiter.api.Assertions.*;
21+
import static org.mockito.Mockito.*;
22+
23+
@ExtendWith(MockitoExtension.class)
24+
class SomParticipantServiceTest {
25+
26+
@Mock
27+
private SomParticipantRepository repository;
28+
29+
@InjectMocks
30+
private SomParticipantService service;
31+
32+
// =======================
33+
// Create Tests
34+
// =======================
35+
@Test
36+
@DisplayName("참가자 생성 - 성공")
37+
void createParticipant_success() {
38+
SomParticipantRequestDto request = new SomParticipantRequestDto();
39+
request.setParticipantName("홍길동");
40+
request.setStudentId("20250001");
41+
request.setDepartment("컴퓨터공학과");
42+
request.setGrade("3");
43+
request.setContact("010-1234-5678");
44+
request.setEmail("[email protected]");
45+
request.setGithubLink("https://github.com/username");
46+
request.setPortfolioLink("https://drive.google.com/file");
47+
48+
when(repository.findByStudentId("20250001")).thenReturn(Optional.empty());
49+
when(repository.save(any(SomParticipant.class))).thenAnswer(invocation -> invocation.getArgument(0));
50+
51+
SomParticipantResponseDto response = service.createParticipant(request);
52+
53+
assertNotNull(response);
54+
assertEquals("홍길동", response.getParticipantName());
55+
assertEquals("20250001", response.getStudentId());
56+
assertEquals("컴퓨터공학과", response.getDepartment());
57+
assertEquals("3", response.getGrade());
58+
assertEquals("010-1234-5678", response.getContact());
59+
assertEquals("[email protected]", response.getEmail());
60+
assertEquals("https://github.com/username", response.getGithubLink());
61+
assertEquals("https://drive.google.com/file", response.getPortfolioLink());
62+
63+
verify(repository, times(1)).findByStudentId("20250001");
64+
verify(repository, times(1)).save(any(SomParticipant.class));
65+
}
66+
67+
@Test
68+
@DisplayName("참가자 생성 - 실패 (학생ID 중복)")
69+
void createParticipant_fail_duplicateStudentId() {
70+
SomParticipantRequestDto request = new SomParticipantRequestDto();
71+
request.setStudentId("20250001");
72+
73+
when(repository.findByStudentId("20250001")).thenReturn(Optional.of(mock(SomParticipant.class)));
74+
75+
CustomException exception = assertThrows(CustomException.class, () -> service.createParticipant(request));
76+
assertEquals(ErrorCode.DUPLICATED_STUDENT_NO, exception.getErrorCode());
77+
78+
verify(repository, times(1)).findByStudentId("20250001");
79+
verify(repository, never()).save(any());
80+
}
81+
82+
// =======================
83+
// Read Tests
84+
// =======================
85+
@Test
86+
@DisplayName("모든 참가자 조회")
87+
void getAllParticipants_success() {
88+
SomParticipant p1 = SomParticipant.builder()
89+
.participantName("홍길동")
90+
.studentId("20250001")
91+
.department("컴퓨터공학과")
92+
.grade("3")
93+
.contact("010-1234-5678")
94+
95+
.githubLink("https://github.com/hong")
96+
.portfolioLink("https://drive.google.com/file")
97+
.build();
98+
SomParticipant p2 = SomParticipant.builder()
99+
.participantName("김철수")
100+
.studentId("20250002")
101+
.department("소프트웨어공학과")
102+
.grade("2")
103+
.contact("010-9876-5432")
104+
105+
.githubLink("https://github.com/kim")
106+
.portfolioLink("https://notion.site")
107+
.build();
108+
109+
when(repository.findAll()).thenReturn(List.of(p1, p2));
110+
111+
List<SomParticipantResponseDto> list = service.getAllParticipants();
112+
113+
assertEquals(2, list.size());
114+
115+
assertEquals("홍길동", list.get(0).getParticipantName());
116+
assertEquals("김철수", list.get(1).getParticipantName());
117+
118+
assertEquals("https://github.com/hong", list.get(0).getGithubLink());
119+
assertEquals("https://notion.site", list.get(1).getPortfolioLink());
120+
121+
verify(repository, times(1)).findAll();
122+
}
123+
124+
@Test
125+
@DisplayName("특정 참가자 조회 - 성공")
126+
void getParticipant_success() {
127+
SomParticipant participant = SomParticipant.builder()
128+
.participantName("홍길동")
129+
.studentId("20250001")
130+
.department("컴퓨터공학과")
131+
.grade("3")
132+
.contact("010-1234-5678")
133+
134+
.githubLink("https://github.com/username")
135+
.portfolioLink("https://drive.google.com/file")
136+
.build();
137+
138+
when(repository.findById(1L)).thenReturn(Optional.of(participant));
139+
140+
SomParticipantResponseDto response = service.getParticipant(1L);
141+
142+
assertEquals("홍길동", response.getParticipantName());
143+
assertEquals("20250001", response.getStudentId());
144+
145+
verify(repository, times(1)).findById(1L);
146+
}
147+
148+
@Test
149+
@DisplayName("특정 참가자 조회 - 실패 (없음)")
150+
void getParticipant_fail_notFound() {
151+
when(repository.findById(1L)).thenReturn(Optional.empty());
152+
153+
CustomException exception = assertThrows(CustomException.class, () -> service.getParticipant(1L));
154+
assertEquals(ErrorCode.NOT_FOUND_PARTICIPANT, exception.getErrorCode());
155+
156+
verify(repository, times(1)).findById(1L);
157+
}
158+
159+
// =======================
160+
// Delete Tests
161+
// =======================
162+
@Test
163+
@DisplayName("참가자 삭제 - 성공")
164+
void deleteParticipant_success() {
165+
SomParticipant participant = SomParticipant.builder().build();
166+
when(repository.findById(1L)).thenReturn(Optional.of(participant));
167+
168+
assertDoesNotThrow(() -> service.deleteParticipant(1L));
169+
verify(repository, times(1)).deleteById(1L);
170+
}
171+
172+
@Test
173+
@DisplayName("참가자 삭제 - 실패 (없음)")
174+
void deleteParticipant_fail_notFound() {
175+
when(repository.findById(1L)).thenReturn(Optional.empty());
176+
177+
CustomException exception = assertThrows(CustomException.class, () -> service.deleteParticipant(1L));
178+
assertEquals(ErrorCode.NOT_FOUND_PARTICIPANT, exception.getErrorCode());
179+
180+
verify(repository, never()).deleteById(any());
181+
}
182+
}

0 commit comments

Comments
 (0)