We are running our component test-cases in which we load some data using cache.. Now the problem is when we try other test-cases, we want to reset the cache because it then doesn't test with the other data. How can we achieve this. We are using spring boot with Java and using Ehcache.
Asked
Active
Viewed 390 times
1 Answers
1
You can inject the org.springframework.cache.CacheManager
bean into your tests and use it to clear the cache before or after each test. Assuming there is a cache named testCache
, a test class that clears the cache would look something like below:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
@SpringBootTest
public class IntegrationTest {
@Autowired
private CacheManager cacheManager;
@BeforeEach
public void setup() {
cacheManager.get("testCache").clear();
}
@Test
public void testSomething() {
}
}
You can find a reference spock-based test that does the same thing on github

devatherock
- 2,423
- 1
- 8
- 23