diff --git a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/twolevelcache/CacheConfig.java b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/twolevelcache/CacheConfig.java index 2072f5806a..67d857eb24 100644 --- a/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/twolevelcache/CacheConfig.java +++ b/spring-boot-modules/spring-caching-2/src/main/java/com/baeldung/caching/twolevelcache/CacheConfig.java @@ -35,7 +35,7 @@ public class CacheConfig { @Bean public CaffeineCache caffeineCacheConfig() { return new CaffeineCache("customerCache", Caffeine.newBuilder() - .expireAfterWrite(Duration.ofSeconds(1)) + .expireAfterWrite(Duration.ofSeconds(3)) .initialCapacity(1) .maximumSize(2000) .build()); diff --git a/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/twolevelcache/CustomerServiceCachingIntegrationTest.java b/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/twolevelcache/CustomerServiceCachingIntegrationTest.java index 6aa3242c95..b28c70da0c 100644 --- a/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/twolevelcache/CustomerServiceCachingIntegrationTest.java +++ b/spring-boot-modules/spring-caching-2/src/test/java/com/baeldung/caching/twolevelcache/CustomerServiceCachingIntegrationTest.java @@ -16,6 +16,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Import; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; import redis.embedded.RedisServer; @@ -30,6 +31,7 @@ import java.util.concurrent.TimeUnit; @ExtendWith(SpringExtension.class) @ImportAutoConfiguration(classes = { CacheAutoConfiguration.class, RedisAutoConfiguration.class }) @EnableCaching +@TestPropertySource(properties = {"spring.redis.port=7000"}) class CustomerServiceCachingIntegrationTest { @MockBean @@ -84,7 +86,8 @@ class CustomerServiceCachingIntegrationTest { .willReturn(Optional.of(customer)); Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID); - TimeUnit.SECONDS.sleep(2); + TimeUnit.SECONDS.sleep(3); + assertThat(customerFromCaffeineCache(CUSTOMER_ID)).isEqualTo(null); Customer customerCacheHit = customerService.getCustomer(CUSTOMER_ID); verify(customerRepository, times(1)).findById(CUSTOMER_ID); @@ -110,7 +113,7 @@ class CustomerServiceCachingIntegrationTest { private final RedisServer redisServer; public EmbeddedRedisConfiguration() throws IOException { - this.redisServer = new RedisServer(); + this.redisServer = new RedisServer(7000); } @PostConstruct