fixed int test issue with redis server

This commit is contained in:
saikat 2024-03-02 09:43:32 +05:30
parent 03f52e6329
commit f959e7a273
2 changed files with 6 additions and 3 deletions

View File

@ -35,7 +35,7 @@ public class CacheConfig {
@Bean @Bean
public CaffeineCache caffeineCacheConfig() { public CaffeineCache caffeineCacheConfig() {
return new CaffeineCache("customerCache", Caffeine.newBuilder() return new CaffeineCache("customerCache", Caffeine.newBuilder()
.expireAfterWrite(Duration.ofSeconds(1)) .expireAfterWrite(Duration.ofSeconds(3))
.initialCapacity(1) .initialCapacity(1)
.maximumSize(2000) .maximumSize(2000)
.build()); .build());

View File

@ -16,6 +16,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import redis.embedded.RedisServer; import redis.embedded.RedisServer;
@ -30,6 +31,7 @@ import java.util.concurrent.TimeUnit;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@ImportAutoConfiguration(classes = { CacheAutoConfiguration.class, RedisAutoConfiguration.class }) @ImportAutoConfiguration(classes = { CacheAutoConfiguration.class, RedisAutoConfiguration.class })
@EnableCaching @EnableCaching
@TestPropertySource(properties = {"spring.redis.port=7000"})
class CustomerServiceCachingIntegrationTest { class CustomerServiceCachingIntegrationTest {
@MockBean @MockBean
@ -84,7 +86,8 @@ class CustomerServiceCachingIntegrationTest {
.willReturn(Optional.of(customer)); .willReturn(Optional.of(customer));
Customer customerCacheMiss = customerService.getCustomer(CUSTOMER_ID); 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); Customer customerCacheHit = customerService.getCustomer(CUSTOMER_ID);
verify(customerRepository, times(1)).findById(CUSTOMER_ID); verify(customerRepository, times(1)).findById(CUSTOMER_ID);
@ -110,7 +113,7 @@ class CustomerServiceCachingIntegrationTest {
private final RedisServer redisServer; private final RedisServer redisServer;
public EmbeddedRedisConfiguration() throws IOException { public EmbeddedRedisConfiguration() throws IOException {
this.redisServer = new RedisServer(); this.redisServer = new RedisServer(7000);
} }
@PostConstruct @PostConstruct