-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Nexters/feature/25-redis-test
redis provider 테스트코드 추가
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/test/java/com/pcb/audy/global/redis/RedisProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.pcb.audy.global.redis; | ||
|
||
import static java.lang.Boolean.TRUE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.pcb.audy.test.RedisTest; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RedisProviderTest implements RedisTest { | ||
@InjectMocks private RedisProvider redisProvider; | ||
|
||
@Mock private RedisTemplate<String, Object> redisTemplate; | ||
@Mock private ValueOperations<String, Object> valueOperations; | ||
|
||
@Test | ||
@DisplayName("데이터 저장 테스트") | ||
void 데이터_저장() { | ||
// given | ||
when(redisTemplate.hasKey(any())).thenReturn(TRUE); | ||
when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
|
||
// when | ||
redisProvider.set(TEST_KEY, TEST_VALUE, TEST_EXPIRE_TIME); | ||
|
||
// then | ||
verify(redisTemplate).hasKey(any()); | ||
verify(redisTemplate).delete(anyString()); | ||
verify(redisTemplate).opsForValue(); | ||
verify(valueOperations).set(any(), any(), any()); | ||
} | ||
|
||
@Test | ||
@DisplayName("데이터 조회 테스트") | ||
void 데이터_조회() { | ||
// given | ||
when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
when(valueOperations.get(any())).thenReturn(TEST_VALUE); | ||
|
||
// when | ||
String value = (String) redisProvider.get(TEST_KEY); | ||
|
||
// then | ||
verify(redisTemplate).opsForValue(); | ||
verify(valueOperations).get(any()); | ||
assertThat(value).isEqualTo(TEST_VALUE); | ||
} | ||
|
||
@Test | ||
@DisplayName("데이터 삭제 테스트") | ||
void 데이터_삭제() { | ||
// given | ||
|
||
// when | ||
redisProvider.delete(TEST_KEY); | ||
|
||
// then | ||
verify(redisTemplate).delete(anyString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("데이터 존재 확인 테스트") | ||
void 데이터_존재_확인() { | ||
// given | ||
when(redisTemplate.hasKey(any())).thenReturn(TRUE); | ||
|
||
// when | ||
Boolean result = redisProvider.hasKey(TEST_KEY); | ||
|
||
// then | ||
verify(redisTemplate).hasKey(any()); | ||
assertThat(result).isEqualTo(TRUE); | ||
} | ||
} |