|
17 | 17 | package org.springframework.cache.interceptor;
|
18 | 18 |
|
19 | 19 | import org.apache.commons.logging.Log;
|
| 20 | +import org.junit.jupiter.api.BeforeEach; |
20 | 21 | import org.junit.jupiter.api.Test;
|
21 |
| -import org.junit.jupiter.api.extension.ExtendWith; |
22 |
| -import org.mockito.Mock; |
23 |
| -import org.mockito.junit.jupiter.MockitoExtension; |
24 | 22 |
|
| 23 | +import org.springframework.cache.Cache; |
25 | 24 | import org.springframework.cache.support.NoOpCache;
|
26 | 25 |
|
| 26 | +import static org.mockito.BDDMockito.given; |
| 27 | +import static org.mockito.Mockito.mock; |
27 | 28 | import static org.mockito.Mockito.verify;
|
28 | 29 |
|
29 | 30 | /**
|
|
32 | 33 | * @author Adam Ostrožlík
|
33 | 34 | * @author Stephane Nicoll
|
34 | 35 | * @author Vedran Pavic
|
| 36 | + * @author Sam Brannen |
35 | 37 | */
|
36 |
| -@ExtendWith(MockitoExtension.class) |
37 | 38 | class LoggingCacheErrorHandlerTests {
|
38 | 39 |
|
39 |
| - @Mock |
40 |
| - private Log logger; |
| 40 | + private static final Cache CACHE = new NoOpCache("NOOP"); |
| 41 | + |
| 42 | + private static final String KEY = "enigma"; |
| 43 | + |
| 44 | + private final Log logger = mock(Log.class); |
| 45 | + |
| 46 | + private LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false); |
| 47 | + |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void setUp() { |
| 51 | + given(this.logger.isWarnEnabled()).willReturn(true); |
| 52 | + } |
41 | 53 |
|
42 | 54 |
|
43 | 55 | @Test
|
44 | 56 | void handleGetCacheErrorLogsAppropriateMessage() {
|
45 |
| - LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false); |
46 |
| - handler.handleCacheGetError(new RuntimeException(), new NoOpCache("NOOP"), "key"); |
47 |
| - verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'key'"); |
| 57 | + this.handler.handleCacheGetError(new RuntimeException(), CACHE, KEY); |
| 58 | + verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'enigma'"); |
48 | 59 | }
|
49 | 60 |
|
50 | 61 | @Test
|
51 | 62 | void handlePutCacheErrorLogsAppropriateMessage() {
|
52 |
| - LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false); |
53 |
| - handler.handleCachePutError(new RuntimeException(), new NoOpCache("NOOP"), "key", new Object()); |
54 |
| - verify(this.logger).warn("Cache 'NOOP' failed to put entry with key 'key'"); |
| 63 | + this.handler.handleCachePutError(new RuntimeException(), CACHE, KEY, null); |
| 64 | + verify(this.logger).warn("Cache 'NOOP' failed to put entry with key 'enigma'"); |
55 | 65 | }
|
56 | 66 |
|
57 | 67 | @Test
|
58 | 68 | void handleEvictCacheErrorLogsAppropriateMessage() {
|
59 |
| - LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false); |
60 |
| - handler.handleCacheEvictError(new RuntimeException(), new NoOpCache("NOOP"), "key"); |
61 |
| - verify(this.logger).warn("Cache 'NOOP' failed to evict entry with key 'key'"); |
| 69 | + this.handler.handleCacheEvictError(new RuntimeException(), CACHE, KEY); |
| 70 | + verify(this.logger).warn("Cache 'NOOP' failed to evict entry with key 'enigma'"); |
62 | 71 | }
|
63 | 72 |
|
64 | 73 | @Test
|
65 | 74 | void handleClearErrorLogsAppropriateMessage() {
|
66 |
| - LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false); |
67 |
| - handler.handleCacheClearError(new RuntimeException(), new NoOpCache("NOOP")); |
| 75 | + this.handler.handleCacheClearError(new RuntimeException(), CACHE); |
68 | 76 | verify(this.logger).warn("Cache 'NOOP' failed to clear entries");
|
69 | 77 | }
|
70 | 78 |
|
71 | 79 | @Test
|
72 |
| - void handleCacheErrorWithStacktrace() { |
73 |
| - LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, true); |
| 80 | + void handleGetCacheErrorWithStackTraceLoggingEnabled() { |
| 81 | + this.handler = new LoggingCacheErrorHandler(this.logger, true); |
74 | 82 | RuntimeException exception = new RuntimeException();
|
75 |
| - handler.handleCacheGetError(exception, new NoOpCache("NOOP"), "key"); |
76 |
| - verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'key'", exception); |
| 83 | + this.handler.handleCacheGetError(exception, CACHE, KEY); |
| 84 | + verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'enigma'", exception); |
77 | 85 | }
|
78 | 86 |
|
79 | 87 | }
|
0 commit comments