Skip to content

Commit 387235d

Browse files
committed
Test: WebSocketMessageControllerTest 예외 처리 로직 적용
1 parent 6937491 commit 387235d

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

src/test/java/com/back/global/websocket/controller/WebSocketMessageControllerTest.java

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.security.Principal;
2020

21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.mockito.ArgumentMatchers.*;
2223
import static org.mockito.Mockito.*;
2324

@@ -89,39 +90,37 @@ void t2() {
8990
}
9091

9192
@Test
92-
@DisplayName("실패 - CustomException 발생")
93+
@DisplayName("실패 - CustomException 발생 시 예외를 그대로 던짐")
9394
void t3() {
9495
// given
9596
Principal mockPrincipal = createMockPrincipal(userId);
96-
CustomException exception = new CustomException(ErrorCode.BAD_REQUEST);
97-
doThrow(exception).when(sessionManager).updateLastActivity(userId);
97+
CustomException expectedException = new CustomException(ErrorCode.BAD_REQUEST);
98+
doThrow(expectedException).when(sessionManager).updateLastActivity(userId);
9899

99-
// when
100-
controller.handleHeartbeat(mockPrincipal, headerAccessor);
100+
// when & then
101+
assertThrows(CustomException.class, () -> {
102+
controller.handleHeartbeat(mockPrincipal, headerAccessor);
103+
});
101104

102-
// then
103105
verify(sessionManager).updateLastActivity(userId);
104-
verify(errorHelper).sendCustomExceptionToUser(sessionId, exception);
106+
verifyNoInteractions(errorHelper);
105107
}
106108

107109
@Test
108-
@DisplayName("실패 - 일반 Exception 발생")
110+
@DisplayName("실패 - 일반 Exception 발생 시 예외를 그대로 던짐")
109111
void t4() {
110112
// given
111113
Principal mockPrincipal = createMockPrincipal(userId);
112-
RuntimeException exception = new RuntimeException("예상치 못한 오류");
113-
doThrow(exception).when(sessionManager).updateLastActivity(userId);
114+
RuntimeException expectedException = new RuntimeException("예상치 못한 오류");
115+
doThrow(expectedException).when(sessionManager).updateLastActivity(userId);
114116

115-
// when
116-
controller.handleHeartbeat(mockPrincipal, headerAccessor);
117+
// when & then
118+
assertThrows(RuntimeException.class, () -> {
119+
controller.handleHeartbeat(mockPrincipal, headerAccessor);
120+
});
117121

118-
// then
119122
verify(sessionManager).updateLastActivity(userId);
120-
verify(errorHelper).sendGenericErrorToUser(
121-
eq(sessionId),
122-
any(Exception.class),
123-
eq("Heartbeat 처리 중 오류가 발생했습니다")
124-
);
123+
verifyNoInteractions(errorHelper);
125124
}
126125
}
127126

@@ -155,45 +154,41 @@ void t2() {
155154

156155
// then
157156
verify(sessionManager, never()).updateLastActivity(any());
158-
159-
// handleActivity의 else 블록에 맞춰 검증 로직 수정
160157
verify(errorHelper).sendInvalidRequestError(sessionId, "사용자 ID가 필요합니다");
161158
}
162159

163160
@Test
164-
@DisplayName("실패 - CustomException 발생")
161+
@DisplayName("실패 - CustomException 발생 시 예외를 그대로 던짐")
165162
void t3() {
166163
// given
167164
Principal mockPrincipal = createMockPrincipal(userId);
168-
CustomException exception = new CustomException(ErrorCode.BAD_REQUEST);
169-
doThrow(exception).when(sessionManager).updateLastActivity(userId);
165+
CustomException expectedException = new CustomException(ErrorCode.BAD_REQUEST);
166+
doThrow(expectedException).when(sessionManager).updateLastActivity(userId);
170167

171-
// when
172-
controller.handleActivity(mockPrincipal, headerAccessor);
168+
// when & then
169+
assertThrows(CustomException.class, () -> {
170+
controller.handleActivity(mockPrincipal, headerAccessor);
171+
});
173172

174-
// then
175173
verify(sessionManager).updateLastActivity(userId);
176-
verify(errorHelper).sendCustomExceptionToUser(sessionId, exception);
174+
verifyNoInteractions(errorHelper);
177175
}
178176

179177
@Test
180-
@DisplayName("실패 - 일반 Exception 발생")
178+
@DisplayName("실패 - 일반 Exception 발생 시 예외를 그대로 던짐")
181179
void t4() {
182180
// given
183181
Principal mockPrincipal = createMockPrincipal(userId);
184-
RuntimeException exception = new RuntimeException("예상치 못한 오류");
185-
doThrow(exception).when(sessionManager).updateLastActivity(userId);
182+
RuntimeException expectedException = new RuntimeException("예상치 못한 오류");
183+
doThrow(expectedException).when(sessionManager).updateLastActivity(userId);
186184

187-
// when
188-
controller.handleActivity(mockPrincipal, headerAccessor);
185+
// when & then
186+
assertThrows(RuntimeException.class, () -> {
187+
controller.handleActivity(mockPrincipal, headerAccessor);
188+
});
189189

190-
// then
191190
verify(sessionManager).updateLastActivity(userId);
192-
verify(errorHelper).sendGenericErrorToUser(
193-
eq(sessionId),
194-
any(Exception.class),
195-
eq("활동 신호 처리 중 오류가 발생했습니다")
196-
);
191+
verifyNoInteractions(errorHelper);
197192
}
198193
}
199194
}

0 commit comments

Comments
 (0)