You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring缓存实际生效但测试失效求助(非Spring Boot)

Spring非Boot环境下缓存测试场景失效问题

我在非Spring Boot的Spring框架服务开发中,计划将原本的Guava缓存切换为Spring自带缓存机制。该缓存在生产运行时可正常生效,但在测试场景中完全不工作。

我已搭建最小复现项目,相关代码、Gradle配置及目录结构如下:

测试代码

package com.somepkg;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
//@ComponentScan("com.somepkg")
@ContextConfiguration(classes = TestConfig.class)
public class SomeTest {
    @Autowired MyApp myApp;

    @Test
    public void testStuff() {
        long start, stop, totalTime;

        start = System.currentTimeMillis();
        myApp.run("stuff");
        stop = System.currentTimeMillis();
        totalTime = stop - start;
        System.out.println("Total: " + totalTime);

        assertThat("Call took too long", totalTime < 1100);
    }
}

@Configuration
@EnableCaching
class TestConfig {
    @Bean MyApp myApp() { return new MyApp(new MyService()); }
    @Bean CacheManager cacheManager() { return new ConcurrentMapCacheManager("greeting"); }
}

应用代码

package com.somepkg;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.stereotype.Component;

@Configuration
@ComponentScan
@EnableCaching
public class Main {
    public static void main(String[] args) {
        try (GenericApplicationContext applicationContext = new AnnotationConfigApplicationContext(Main.class)) {
            MyApp app = applicationContext.getBean(MyApp.class);
            app.run("Spring");
        }
    }

    @Bean CacheManager cacheManager() { return new ConcurrentMapCacheManager("greeting"); }
}

@Component
class MyApp {
    private final MyService myService;

    public MyApp(MyService myService) { this.myService = myService; }

    public void run(String message) {
        long start, stop, totalTime;

        System.out.println("First call to service...");
        start = System.currentTimeMillis();
        myService.hello(message);
        stop = System.currentTimeMillis();
        totalTime = stop - start;

        System.out.println("Second call to service...");
        start = System.currentTimeMillis();
        myService.hello(message);
        stop = System.currentTimeMillis();
        totalTime += stop - start;

        System.out.println("done in " + totalTime);
    }
}

@Component
class MyService {
    @Cacheable("greeting")
    public String hello(String message) {
        try { Thread.sleep(1000); } catch (Exception e) {}
        return "Hello " + message + "!";
    }
}

build.gradle配置

mainClassName = 'com.somepkg.Main'

run { standardInput = System.in }

repositories { jcenter() }

dependencies {
    implementation 'org.springframework:spring-context:5.3.17'
    implementation 'org.springframework:spring-test:5.3.17'
    implementation 'junit:junit:4.13.1'
    implementation 'org.hamcrest:hamcrest:2.2'
}

目录结构

├── build.gradle
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── somepkg
    │               └── Main.java
    └── test
        └── java
            └── com
                └── somepkg
                    └── SomeTest.java

我已通过谷歌搜索相关内容,但未找到有效解决方案,且确认已配置各类教程要求的所有项。请提供技术帮助,注意使用Spring Boot并非可行方案。

内容的提问来源于stack exchange,提问作者Erik Vesterlund

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:53:15