1

私の Spring-Boot Web アプリケーション プロジェクトでは、Spring Cache を使用してキャッシュを実装しています。で定義された構成キーによって、キャッシュを有効/無効にすることができますapplication.yml。キャッシュがないことを前提としてテストが記述されている既存のテスト ケースが既にあります。integration-testしたがって、私のプロファイルではデフォルトでキャッシングが無効になっており、初期化するNoOpCacheManagerとすべてのテストが機能します。

@Profile(value = { "default", "production", "integration-test" })
@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)
public class CacheBeanConfig extends CachingConfigurerSupport {

    @Autowired
    private CacheConfig cacheConfig;

    @Bean
    @Override
    public CacheManager cacheManager() {
        if (cacheConfig.isEnabled()) {
            System.out.println("****************Couchbase CacheBeanTestsConfig cache init.**********************");
            Map<String, DeclaraCouchbaseTemplate> cacheCouchTemplateMap = Maps.newHashMap();
            Map<String, Integer> cacheTtlMap = Maps.newHashMap();

            for (CacheConfig.CacheConfigParam cacheParam : cacheConfig.getCaches()) {
                try {
                    cacheCouchTemplateMap.put(cacheParam.getName(),
                        couchbaseManager.getCouchbaseTemplate(cacheParam.getName()));
                    cacheTtlMap.put(cacheParam.getName(), cacheParam.getTtl());
                } catch (IOException | URISyntaxException e) {
                    throw new FaultException("Unable to get couchbase template.");
                }
            }

            return new CouchbaseCacheManager(cacheCouchTemplateMap, cacheTtlMap, metricRegistry);
        } else {
            System.out.println("****************NoOp CacheBeanTestsConfig cache init.**********************");
            NoOpCacheManager noopCacheManager = new NoOpCacheManager();
            return noopCacheManager;
        }
    }

}

また、キャッシング機能を検証するためのテストも書きたいと思っています。CachedControllerTestすべてのキャッシュ固有のテストが記述されているクラスを作成しました。

問題は私が走るときです

mvn test -Dtest=CachedControllersTest -Dspring.profiles.active=integration-test

Bean 関数でキャッシュを有効にしたにもかかわらず、キャッシュ マネージャーが初期化されているため、クラスのすべてのテストCachedControllerTestが失敗しています。NoOpCacheManager

別のプロファイルを作成しようとしましたが、 Bean が初期化されるとリセットされないCachedControllerTestため、まだ失敗します。cacheManager

mvn test -Dtest=CachedControllersTest -Dspring.profiles.active=integration-test,integration-cache-test

ここに私の CachedControllerTest クラスがあります

@ActiveProfiles("integration-cache-test")
@DirtiesContext
public class CachedControllersTest extends AbstractRestControllerTest {

    @Configuration
    @EnableCaching(mode = AdviceMode.ASPECTJ)
    @Profile("integration-cache-test")
    public static class CachedControllerTestsBeanConfig {

        @Autowired
        private CouchbaseManager couchbaseManager;

        @Autowired
        private CacheConfig cacheConfig;

        @Autowired
        private MetricRegistry metricRegistry;

        @Autowired
        GlobalApplicationConfig globalAppConfig;

        @Bean
        public CacheManager cacheManager() {
            System.out.println("**************** CachedControllerTestsBeanConfig EnabledCaching**********************");
            cacheConfig.setEnabled(true);
            if (cacheConfig.isEnabled()) {
                System.out.println("****************Couchbase CachedControllerTestsBeanConfig cache init.**********************");
                Map<String, DeclaraCouchbaseTemplate> cacheCouchTemplateMap = Maps.newHashMap();
                Map<String, Integer> cacheTtlMap = Maps.newHashMap();

                for (CacheConfig.CacheConfigParam cacheParam : cacheConfig.getCaches()) {
                    try {
                        cacheCouchTemplateMap.put(cacheParam.getName(),
                            couchbaseManager.getCouchbaseTemplate(cacheParam.getName()));
                        cacheTtlMap.put(cacheParam.getName(), cacheParam.getTtl());
                    } catch (IOException | URISyntaxException e) {
                        throw new FaultException("Unable to get couchbase template.");
                    }
                }

                return new CouchbaseCacheManager(cacheCouchTemplateMap, cacheTtlMap, metricRegistry);
            } else {
                System.out.println("****************NoOp CachedControllerTestsBeanConfig cache init.**********************");
                NoOpCacheManager noopCacheManager = new NoOpCacheManager();
                return noopCacheManager;
            }
        }

        @Bean(name = "mtlKeyGenerator")
        public KeyGenerator keyGenerator() {
            System.out.println("****************CachedControllerTestsBeanConfig mtlKeyGenerator.**********************");
            return new MultiTenantKeyGenerator(globalAppConfig.getTenantId());
        }

        @Bean(name = CacheManagementConfigUtils.CACHE_ASPECT_BEAN_NAME)
        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
        public AnnotationGroupCacheAspect cacheAspect() {
            AnnotationGroupCacheAspect cacheAspect = AnnotationGroupCacheAspect.aspectOf();
            CacheManager cacheManager = (CacheManager) StaticContextHolder.getApplicationContext().getBean("cacheManager");
            cacheAspect.setCacheManager(cacheManager);

            KeyGenerator keyGenerator = (KeyGenerator) StaticContextHolder.getApplicationContext().getBean("mtlKeyGenerator");
            cacheAspect.setKeyGenerator(keyGenerator);
            return cacheAspect;
        }
    }

    @Component
    public static class StaticContextHolder implements ApplicationContextAware {

        private static ApplicationContext appContext;

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            appContext = applicationContext;
        }

        public static ApplicationContext getApplicationContext() {
            return appContext;
        }
    }
}

アプリケーション.yml

spring:
    profiles: integration-test

cache:
    enabled: false
---
spring:
    profiles: integration-cache-test

cache:
    enabled: false 

私の要件は、各テスト クラスの cacheManage を再初期化することです。CacheConfig は、適切CacheManagerに初期化できるように実行時に変更する Bean です。

単独でCachedControllerTestクラス テストを実行すると、キャッシュ マネージャーを NoOpCacheManager に初期化する前に他のテスト クラスが実行されないため、それらはすべてパスします。

この状況を機能させるためのヘルプ/提案を事前に感謝します。

編集 1

Sam の提案に基づいて、 を追加しまし@ActiveProfilesた。

編集 2

AbstractRestControllerTest クラスの定義

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class AbstractRestControllerTest {
}
4

1 に答える 1