Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* BadgeCacheInvalidator 의 evict 동작(Redis 키 삭제, SCAN 순회)을 검증한다.
Expand Down Expand Up @@ -48,16 +48,16 @@ class Evict {

@SuppressWarnings("unchecked")
Cursor<String> cursor = (Cursor<String>) mock(Cursor.class);
when(cursor.hasNext()).thenReturn(true, true, false);
when(cursor.next()).thenReturn(key1, key2);
when(redis.scan(any(ScanOptions.class))).thenReturn(cursor);
given(cursor.hasNext()).willReturn(true, true, false);
given(cursor.next()).willReturn(key1, key2);
given(redis.scan(any(ScanOptions.class))).willReturn(cursor);

// when
invalidator.evict(username);

// then — SCAN(badge:octocat:*)으로 찾은 실제 키를 일괄 삭제한다
verify(redis).delete(List.of(key1, key2));
verify(cursor).close();
then(redis).should().delete(List.of(key1, key2));
then(cursor).should().close();
}

@Test
Expand All @@ -68,15 +68,15 @@ class Evict {

@SuppressWarnings("unchecked")
Cursor<String> cursor = (Cursor<String>) mock(Cursor.class);
when(cursor.hasNext()).thenReturn(false);
when(redis.scan(any(ScanOptions.class))).thenReturn(cursor);
given(cursor.hasNext()).willReturn(false);
given(redis.scan(any(ScanOptions.class))).willReturn(cursor);

// when
invalidator.evict(username);

// then
verify(redis, never()).delete(anyCollection());
verify(cursor, times(1)).close();
then(redis).should(never()).delete(anyCollection());
then(cursor).should(times(1)).close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;

import com.tokenphage.api.domain.badge.repository.UserBadgeGrantRepository;
import com.tokenphage.api.domain.badge.repository.projection.BadgeGrantRow;
Expand Down Expand Up @@ -145,7 +145,7 @@ class ResolveGrantTest {
sut.resolveGrant(USERNAME, PUBLIC_CODE);

// then
verify(grantRepo).findGrant(usernameCaptor.capture(), codeCaptor.capture());
then(grantRepo).should().findGrant(usernameCaptor.capture(), codeCaptor.capture());
assertThat(usernameCaptor.getValue()).isEqualTo(USERNAME);
assertThat(codeCaptor.getValue()).isEqualTo(PUBLIC_CODE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;

/**
* UserService.saveUser 동작을 검증한다.
Expand All @@ -33,6 +33,6 @@ class UserServiceTest {
userService.saveUser(githubId, username);

// then
verify(userRepo).upsert(githubId, username);
then(userRepo).should().upsert(githubId, username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* GlobalExceptionHandler의 예외 → HTTP 응답 매핑을 검증한다.
Expand All @@ -31,8 +31,8 @@ class GlobalExceptionHandlerTest {
// given
MethodArgumentNotValidException ex = mock(MethodArgumentNotValidException.class);
BindingResult bindingResult = mock(BindingResult.class);
when(ex.getBindingResult()).thenReturn(bindingResult);
when(bindingResult.getFieldErrors()).thenReturn(List.of(
given(ex.getBindingResult()).willReturn(bindingResult);
given(bindingResult.getFieldErrors()).willReturn(List.of(
new FieldError("syncRequest", "deviceId", "deviceId must be a valid UUID")));

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ void tearDown() {
@DisplayName("캡처_성공응답_상태와기능정보기록")
void 캡처_성공응답_상태와기능정보기록() throws Throwable {
// given
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -100,7 +101,7 @@ void tearDown() {
// given
// 200으로 응답하지만 도메인상 거부인 경우(자격 없는 배지)를 구분하기 위한 통로다.
currentRequest().setAttribute(AuditOutcome.ATTRIBUTE_KEY, AuditOutcome.BADGE_GRANT_DENIED);
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -117,7 +118,7 @@ void tearDown() {
// given
// AuditOutcome 타입만 받는다. 문자열을 심어도 무시하고 기본값으로 떨어져야 한다.
currentRequest().setAttribute(AuditOutcome.ATTRIBUTE_KEY, "badge_grant_denied");
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -130,7 +131,7 @@ void tearDown() {
@DisplayName("캡처_결과미지정_success로기록")
void 캡처_결과미지정_success로기록() throws Throwable {
// given
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -147,7 +148,7 @@ void tearDown() {
MockHttpServletRequest request = currentRequest();
request.setRequestURI("/badge/example-user");
request.setQueryString("theme=contributor&mode=dark");
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -165,7 +166,7 @@ void tearDown() {
MockHttpServletRequest request = currentRequest();
request.setRequestURI("/badge/example-user");
request.setQueryString("theme=" + "x".repeat(400));
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -186,7 +187,7 @@ private MockHttpServletRequest currentRequest() {
@DisplayName("캡처_AppException_에러코드상태와코드기록후재전파")
void 캡처_AppException_에러코드상태와코드기록후재전파() throws Throwable {
// given — gist 도용 시나리오(401, AUTH_007)
when(joinPoint.proceed()).thenThrow(new AppException(AuthErrorCode.OWNER_MISMATCH));
given(joinPoint.proceed()).willThrow(new AppException(AuthErrorCode.OWNER_MISMATCH));

// when / then — 예외는 그대로 재전파되어야 한다
assertThatThrownBy(() -> aspect.audit(joinPoint)).isInstanceOf(AppException.class);
Expand All @@ -200,7 +201,7 @@ private MockHttpServletRequest currentRequest() {
@DisplayName("캡처_미인증요청_식별필드null")
void 캡처_미인증요청_식별필드null() throws Throwable {
// given — SecurityContext 미설정
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -222,7 +223,7 @@ private MockHttpServletRequest currentRequest() {
.build();
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(jwt, null));
when(joinPoint.proceed()).thenReturn(ResponseEntity.ok().build());
given(joinPoint.proceed()).willReturn(ResponseEntity.ok().build());

// when
aspect.audit(joinPoint);
Expand All @@ -235,7 +236,7 @@ private MockHttpServletRequest currentRequest() {

private RequestAuditCommand captureRecorded() {
ArgumentCaptor<RequestAuditCommand> captor = ArgumentCaptor.forClass(RequestAuditCommand.class);
verify(requestAuditService).record(captor.capture());
then(requestAuditService).should().record(captor.capture());
return captor.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
import static org.mockito.BDDMockito.willThrow;

/**
* RequestAuditService.record 의 매핑 위임과 best-effort 예외 처리를 검증한다(실 DB 미접촉).
Expand Down Expand Up @@ -44,7 +44,7 @@ class RequestAuditServiceTest {

// then
ArgumentCaptor<RequestAuditLog> captor = ArgumentCaptor.forClass(RequestAuditLog.class);
verify(auditLogRepo).save(captor.capture());
then(auditLogRepo).should().save(captor.capture());
RequestAuditLog saved = captor.getValue();
assertThat(saved.getFeature()).isEqualTo("auth");
assertThat(saved.getAction()).isEqualTo("verify");
Expand All @@ -66,7 +66,7 @@ class RequestAuditServiceTest {
RequestAuditCommand command = new RequestAuditCommand(
"sync", "sync", "POST", "/api/sync",
500, 10, "203.0.113.8", -1002L, "octocat", "tokenphage-cli/1.0", "error");
doThrow(new RuntimeException("db down")).when(auditLogRepo).save(any());
willThrow(new RuntimeException("db down")).given(auditLogRepo).save(any());

// when / then — best-effort: 예외를 전파하지 않는다
assertThatCode(() -> requestAuditService.record(command)).doesNotThrowAnyException();
Expand All @@ -87,7 +87,7 @@ class RequestAuditServiceTest {

// then — 컬럼 한계(255)로 잘려 저장된다(INSERT 실패·유실 방지)
ArgumentCaptor<RequestAuditLog> captor = ArgumentCaptor.forClass(RequestAuditLog.class);
verify(auditLogRepo).save(captor.capture());
then(auditLogRepo).should().save(captor.capture());
RequestAuditLog saved = captor.getValue();
assertThat(saved.getUserAgent()).hasSize(255);
assertThat(saved.getRequestPath()).hasSize(255);
Expand All @@ -106,7 +106,7 @@ class RequestAuditServiceTest {

// then
ArgumentCaptor<RequestAuditLog> captor = ArgumentCaptor.forClass(RequestAuditLog.class);
verify(auditLogRepo).save(captor.capture());
then(auditLogRepo).should().save(captor.capture());
RequestAuditLog saved = captor.getValue();
assertThat(saved.getGithubId()).isNull();
assertThat(saved.getUsername()).isNull();
Expand Down
Loading
Loading