首页 > 解决方案 > 如何在方法上测试 Spring 的 Cacheable?

问题描述

最近在我的项目中,我被要求将 Spring 的 @Cacheable 注释用于从数据库返回静态 referenceData 的方法之一。我关注了这个博客https://medium.com/@d.lopez.j/configuring-multiple-ttl-caches-in-spring-boot-dinamically-75f4aa6809f3,它指导动态创建缓存。我正在尝试通过遵循这个 SO 答案来测试这个实现如何在 Spring Data 存储库上测试 Spring 的声明性缓存支持?我正面临问题。我无法将 application-test.properties 中的属性加载到我的测试类中。

我的 CachingConfig 类

package org.vinodh.testing;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "caching")
@Data
public class CachingConfig {

    private Map<String, CacheSpec> specs;

    @Data
    public static class CacheSpec {
        private int minutesToExpire;
        private int maximumSize;

        public int getMaximumSize() {
            return maximumSize;
        }

        public void setMaximumSize(int maximumSize) {
            this.maximumSize = maximumSize;
        }

        public int getMinutesToExpire() {
            return minutesToExpire;
        }

        public void setMinutesToExpire(int minutesToExpire) {
            this.minutesToExpire = minutesToExpire;
        }

    }

    public Map<String, CacheSpec> getSpecs() {
        return specs;
    }

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        if (specs != null) {
            List<CaffeineCache> caches = specs.entrySet().stream()
                    .map(entry -> buildCache(entry.getKey(), entry.getValue())).collect(Collectors.toList());
            cacheManager.setCaches(caches);
        }
        return cacheManager;
    }

    private CaffeineCache buildCache(String name, CacheSpec specs) {
        Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
                .expireAfterWrite(specs.getMinutesToExpire(), TimeUnit.MINUTES).maximumSize(specs.getMaximumSize());
        return new CaffeineCache(name, caffeineBuilder.build());
    }
}

我的测试班

package org.vinodh.testing;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties(prefix = "caching")
@ActiveProfiles("test")
@Profile("test")
public class CachingConfigTest {

    @Configuration
    @EnableCaching
    static class NestedCacheConfiguration {

    static class CacheSpec {
            @SuppressWarnings("unused")
            private int minutesToExpire;
            @SuppressWarnings("unused")
            private int maximumSize;
        }

        private Map<String, CacheSpec> specs;

        public Map<String, CacheSpec> getSpecs() {
            return specs;
        }

        @Bean
        public CacheManager cacheManager() {
            SimpleCacheManager cacheManager = new SimpleCacheManager();
            System.out.println(getSpecs()); // Is null
            return cacheManager;
        }

    }

    @Test
    public void test() {
        System.out.println("Inside Test");
    }     
}

应用程序-test.properties

caching.specs.test.minutesToExpire=10
caching.specs.test.maximumSize=10

标签: javaspring-bootspring-boot-test

解决方案


推荐阅读