|
4 | 4 | import static org.hamcrest.CoreMatchers.nullValue; |
5 | 5 | import static org.junit.Assert.assertThat; |
6 | 6 |
|
7 | | -import org.baeldung.caching.eviction.CacheEvictionMain; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
8 | 10 | import org.baeldung.caching.eviction.service.CachingService; |
9 | 11 | import org.junit.Before; |
10 | 12 | import org.junit.Test; |
11 | 13 | import org.junit.runner.RunWith; |
12 | 14 | import org.springframework.beans.factory.annotation.Autowired; |
13 | | -import org.springframework.boot.test.context.SpringBootTest; |
14 | | -import org.springframework.test.context.junit4.SpringRunner; |
| 15 | +import org.springframework.cache.Cache; |
| 16 | +import org.springframework.cache.CacheManager; |
| 17 | +import org.springframework.cache.annotation.EnableCaching; |
| 18 | +import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean; |
| 19 | +import org.springframework.cache.support.SimpleCacheManager; |
| 20 | +import org.springframework.context.annotation.Bean; |
| 21 | +import org.springframework.context.annotation.Configuration; |
| 22 | +import org.springframework.test.context.ContextConfiguration; |
| 23 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
15 | 24 |
|
16 | | -@RunWith(SpringRunner.class) |
17 | | -@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class }) |
| 25 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 26 | +@ContextConfiguration |
18 | 27 | public class CacheEvictAnnotationIntegrationTest { |
19 | 28 |
|
20 | | - @Autowired |
21 | | - CachingService cachingService; |
22 | | - |
23 | | - @Before |
24 | | - public void before() { |
25 | | - cachingService.putToCache("first", "key1", "Baeldung"); |
26 | | - cachingService.putToCache("first", "key2", "Article"); |
27 | | - } |
28 | | - |
29 | | - @Test |
30 | | - public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { |
31 | | - cachingService.evictSingleCacheValue("key1"); |
32 | | - String key1 = cachingService.getFromCache("first", "key1"); |
33 | | - assertThat(key1, is(nullValue())); |
34 | | - } |
35 | | - |
36 | | - @Test |
37 | | - public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { |
38 | | - cachingService.evictAllCacheValues(); |
39 | | - String key1 = cachingService.getFromCache("first", "key1"); |
40 | | - String key2 = cachingService.getFromCache("first", "key2"); |
41 | | - assertThat(key1, is(nullValue())); |
42 | | - assertThat(key2, is(nullValue())); |
43 | | - } |
| 29 | + @Configuration |
| 30 | + @EnableCaching |
| 31 | + static class ContextConfiguration { |
| 32 | + |
| 33 | + @Bean |
| 34 | + public CachingService cachingService() { |
| 35 | + return new CachingService(); |
| 36 | + } |
| 37 | + |
| 38 | + @Bean |
| 39 | + public CacheManager cacheManager(){ |
| 40 | + SimpleCacheManager cacheManager = new SimpleCacheManager(); |
| 41 | + List<Cache> caches = new ArrayList<>(); |
| 42 | + caches.add(cacheBean().getObject()); |
| 43 | + cacheManager.setCaches(caches ); |
| 44 | + return cacheManager; |
| 45 | + } |
| 46 | + |
| 47 | + @Bean |
| 48 | + public ConcurrentMapCacheFactoryBean cacheBean(){ |
| 49 | + ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean(); |
| 50 | + cacheFactoryBean.setName("first"); |
| 51 | + return cacheFactoryBean; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Autowired |
| 56 | + CachingService cachingService; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void before() { |
| 60 | + cachingService.putToCache("first", "key1", "Baeldung"); |
| 61 | + cachingService.putToCache("first", "key2", "Article"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { |
| 66 | + cachingService.evictSingleCacheValue("key1"); |
| 67 | + String key1 = cachingService.getFromCache("first", "key1"); |
| 68 | + assertThat(key1, is(nullValue())); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { |
| 73 | + cachingService.evictAllCacheValues(); |
| 74 | + String key1 = cachingService.getFromCache("first", "key1"); |
| 75 | + String key2 = cachingService.getFromCache("first", "key2"); |
| 76 | + assertThat(key1, is(nullValue())); |
| 77 | + assertThat(key2, is(nullValue())); |
| 78 | + } |
44 | 79 | } |
0 commit comments