Files
spring-boot-demo/redis/src/test/java/com/ankurm/redis/RedisCacheDefaultTest.java
T

67 lines
2.3 KiB
Java

package com.ankurm.redis;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import java.util.List;
import com.ankurm.redis.cache.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.test.context.TestPropertySource;
/**
* Redis as the Spring cache, with Boot's defaults. Writes docs/output/14-cache-default.txt.
*/
@RedisTest
@TestPropertySource(properties = "spring.cache.redis.time-to-live=10m")
class RedisCacheDefaultTest {
@Autowired
CacheManager cacheManager;
@Autowired
UserService users;
@BeforeEach
void clean() {
LocalRedis.flush();
users.resetCalls();
}
@Test
void defaultsUseJdkSerialisationForValues() {
try (Transcript t = new Transcript("14-cache-default.txt",
"Redis as the Spring cache: what the defaults store")) {
t.section("Which cache manager did Boot pick?");
t.line("%s", cacheManager.getClass().getName());
t.section("A Serializable result, cached");
users.findSerializable(1);
users.findSerializable(1);
t.line("findSerializable(1) called twice, method body ran %d time(s)", users.calls());
assertThat(users.calls()).isEqualTo(1);
t.cliSorted("KEYS", "*");
List<String> ttl = t.cli("--no-raw", "TTL", "legacy::1");
assertThat(ttl.get(0)).isEqualTo("(integer) 600");
t.cli("--no-raw", "GET", "legacy::1");
t.section("A result that is not Serializable");
t.line("users.find(1)");
Throwable thrown = catchThrowable(() -> users.find(1));
assertThat(thrown).isNotNull();
t.line("%s", thrown.getClass().getName());
t.line(" message: %s", thrown.getMessage());
Throwable root = NestedExceptionUtils.getMostSpecificCause(thrown);
t.line(" root: %s", root.getMessage());
t.line("method body ran %d time(s) in total", users.calls());
t.cliSorted("KEYS", "*");
}
}
}