Skip to content

Commit a5b2086

Browse files
committed
Improve caching documentation with regards to running tests
Closes gh-48529
1 parent 56e41d5 commit a5b2086

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/io/caching.adoc

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
The Spring Framework provides support for transparently adding caching to an application.
55
At its core, the abstraction applies caching to methods, thus reducing the number of executions based on the information available in the cache.
66
The caching logic is applied transparently, without any interference to the invoker.
7+
For more details, check the {url-spring-framework-docs}/integration/cache.html[relevant section] of the Spring Framework reference documentation.
8+
79
Spring Boot auto-configures the cache infrastructure as long as caching support is enabled by using the javadoc:org.springframework.cache.annotation.EnableCaching[format=annotation] annotation.
810

9-
NOTE: Check the {url-spring-framework-docs}/integration/cache.html[relevant section] of the Spring Framework reference for more details.
11+
TIP: Avoid adding javadoc:org.springframework.cache.annotation.EnableCaching[format=annotation] to the main method's application class.
12+
Doing so makes caching a mandatory feature, including xref:io/caching.adoc#io.caching.testing[when running a test suite].
1013

11-
In a nutshell, to add caching to an operation of your service add the relevant annotation to its method, as shown in the following example:
14+
To add caching to an operation of your service add the relevant annotation to its method, as shown in the following example:
1215

1316
include-code::MyMathService[]
1417

@@ -48,10 +51,7 @@ If you have not defined a bean of type javadoc:org.springframework.cache.CacheMa
4851
. xref:io/caching.adoc#io.caching.provider.cache2k[]
4952
. xref:io/caching.adoc#io.caching.provider.simple[]
5053

51-
Additionally, {url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] provides {url-spring-boot-for-apache-geode-docs}#geode-caching-provider[auto-configuration for using Apache Geode as a cache provider].
52-
5354
TIP: If the javadoc:org.springframework.cache.CacheManager[] is auto-configured by Spring Boot, it is possible to _force_ a particular cache provider by setting the configprop:spring.cache.type[] property.
54-
Use this property if you need to xref:io/caching.adoc#io.caching.provider.none[use no-op caches] in certain environments (such as tests).
5555

5656
TIP: Use the `spring-boot-starter-cache` starter to quickly add basic caching dependencies.
5757
The starter brings in `spring-context-support`.
@@ -262,10 +262,6 @@ This is similar to the way the "real" cache providers behave if you use an undec
262262
[[io.caching.provider.none]]
263263
=== None
264264

265-
When javadoc:org.springframework.cache.annotation.EnableCaching[format=annotation] is present in your configuration, a suitable cache configuration is expected as well.
266-
If you have a custom ` org.springframework.cache.CacheManager`, consider defining it in a separate javadoc:org.springframework.context.annotation.Configuration[format=annotation] class so that you can override it if necessary.
267-
None uses a no-op implementation that is useful in tests, and slice tests use that by default via javadoc:org.springframework.boot.test.autoconfigure.core.AutoConfigureCache[format=annotation].
268-
269265
If you need to use a no-op cache rather than the auto-configured cache manager in a certain environment, set the cache type to `none`, as shown in the following example:
270266

271267
[configprops,yaml]
@@ -274,3 +270,30 @@ spring:
274270
cache:
275271
type: "none"
276272
----
273+
274+
275+
276+
[[io.caching.testing]]
277+
== Testing
278+
279+
It is generally useful to use a no-op implementation when running a test suite.
280+
This section lists a number of strategies that are useful for tests.
281+
282+
When a custom javadoc:org.springframework.cache.CacheManager[] is defined, the best option is to make sure that caching configuration is defined in an isolated javadoc:org.springframework.context.annotation.Configuration[format=annotation] class.
283+
Doing so makes sure that caching is not required by slice tests.
284+
For tests that enable a full context, such as javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation], an explicit configuration overriding the regular configuration is required.
285+
286+
If caching is auto-configured, more options are available.
287+
Slice tests are annotated with javadoc:org.springframework.boot.test.autoconfigure.core.AutoConfigureCache[format=annotation], which replaces the auto-configured javadoc:org.springframework.cache.CacheManager[] by a no-op implementation.
288+
Integration tests can also benefit from this feature by annotate the relevant test class as follows:
289+
290+
include-code::MyIntegrationTests[]
291+
292+
Another option is to force a no-op implementation for the auto-configured javadoc:org.springframework.cache.CacheManager[]:
293+
294+
[configprops,yaml]
295+
----
296+
spring:
297+
cache:
298+
type: "none"
299+
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.io.caching.testing;
18+
19+
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
20+
import org.springframework.boot.test.context.SpringBootTest;
21+
22+
@SpringBootTest
23+
@AutoConfigureCache
24+
public class MyIntegrationTests {
25+
26+
// Tests use a no-op cache manager
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.io.caching.testing
18+
19+
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache
20+
import org.springframework.boot.test.context.SpringBootTest
21+
22+
@SpringBootTest
23+
@AutoConfigureCache
24+
class MyIntegrationTests {
25+
26+
// Tests use a no-op cache manager
27+
28+
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/core/AutoConfigureCache.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@
3030
import org.springframework.cache.support.NoOpCacheManager;
3131

3232
/**
33-
* Annotation that can be applied to a test class to configure a test {@link CacheManager}
34-
* if none has been defined yet. By default this annotation installs a
35-
* {@link NoOpCacheManager}.
33+
* Annotation that can be applied to a test class to customize the
34+
* {@linkplain #cacheProvider() cache provider}. By default, a {@link NoOpCacheManager} is
35+
* auto-configured
36+
* <p>
37+
* As for the regular auto-configuration, this has no effect if a custom
38+
* {@link CacheManager} is defined.
3639
*
3740
* @author Phillip Webb
41+
* @author Stephane Nicoll
3842
* @since 1.4.0
3943
*/
4044
@Target(ElementType.TYPE)
@@ -44,6 +48,10 @@
4448
@ImportAutoConfiguration
4549
public @interface AutoConfigureCache {
4650

51+
/**
52+
* The {@link CacheType} to configure, overriding the behavior for testing purposes.
53+
* @return the cache type to configure
54+
*/
4755
@PropertyMapping("spring.cache.type")
4856
CacheType cacheProvider() default CacheType.NONE;
4957

0 commit comments

Comments
 (0)