TechGeek
TechGeek

Reputation: 508

Mocking Cache Config class failing

I haven't mocked the configuration class for cache config properties. Can someone help me with possible unit tests for this cacheable layer ?

CacheConfig.java

import com.github.benmanes.caffeine.cache.Caffeine;

@Configuration
@EnableCaching
public class CacheConfig {
    private final CacheProperties cacheProperties;

    public CacheConfig(CacheProperties cacheProperties) {
        this.cacheProperties = cacheProperties;
    }
    @Bean
    @ConditionalOnProperty(name = "spring.cache.type", havingValue = "caffeine")
    public Caffeine<Object, Object> caffeineConfig() {
        return Caffeine.from(cacheProperties.getCaffeine().getSpec());
    }
}

I tried something like this., but it's not working as expected and throwing errors.Can someone help me take a look into this ?

@SpringBootTest(classes = CacheConfig.class)
public class CacheConfigTest {

    @MockBean
    private CacheProperties cacheProperties;

    @MockBean
    private CacheManager cacheManager;

    @InjectMocks
    private CacheConfig cacheConfig;

    @Value("${spring.cache.type}")
    private String cacheType;

    @BeforeEach
    void setUp() {
        // Mock the behavior of cacheProperties
        when(cacheProperties.getCaffeine()).thenReturn(new CacheProperties.Caffeine());
        when(cacheProperties.getCaffeine().getSpec()).thenReturn("expireAfterWrite=10m");
    }

    @Test
    void testCaffeineConfigBeanCreationWhenCacheTypeIsCaffeine() {
        // Arrange
        System.setProperty("spring.cache.type", "caffeine");

        // Act
        Caffeine<Object, Object> caffeine = cacheConfig.caffeineConfig();

        // Assert
        assertThat(caffeine).isNotNull();
        verify(cacheProperties, times(1)).getCaffeine();
    }

    @Test
    void testCaffeineConfigBeanNotCreatedWhenCacheTypeIsNotCaffeine() {
        // Arrange
        System.setProperty("spring.cache.type", "other");

        // Act
        Caffeine<Object, Object> caffeine = cacheConfig.caffeineConfig();

        // Assert
        assertThat(caffeine).isNull();
    }
}

Exception Stack Trace:

Caused by: java.lang.IllegalStateException: No CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.
    at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:277) ~[spring-context-6.1.6.jar:6.1.6]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:986) ~[spring-beans-6.1.6.jar:6.1.6]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962) ~[spring-context-6.1.6.jar:6.1.6]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) ~[spring-context-6.1.6.jar:6.1.6]

Upvotes: 0

Views: 25

Answers (0)

Related Questions