From dd53ed0aecc5e986ab758490ff160fc8b858318d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 5 Jun 2017 10:55:40 +0200 Subject: [PATCH] Polish "Add Slice test annotation for Redis" Closes gh-9224 --- .../main/asciidoc/spring-boot-features.adoc | 7 ++++- spring-boot-test-autoconfigure/pom.xml | 30 +++++++++---------- .../data/redis/AutoConfigureDataRedis.java | 2 +- .../data/redis/DataRedisTest.java | 9 ++---- .../redis/DataRedisTypeExcludeFilter.java | 3 +- .../main/resources/META-INF/spring.factories | 3 +- .../redis/DataRedisTestIntegrationTests.java | 9 ++++-- ...TestWithIncludeFilterIntegrationTests.java | 6 +++- .../data/redis/ExampleService.java | 8 ++--- .../autoconfigure/data/redis/PersonHash.java | 2 ++ 10 files changed, 44 insertions(+), 35 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index eb6c5b9ca2..03206604bc 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -6010,9 +6010,13 @@ disable transaction management for a test or for the whole class as follows: A list of the auto-configuration that is enabled by `@DataNeo4jTest` can be <>. + + [[boot-features-testing-spring-boot-applications-testing-autoconfigured-redis-test]] ==== Auto-configured Data Redis tests -`@DataRedisTest` can be used if you want to test Redis applications. +`@DataRedisTest` can be used if you want to test Redis applications. By default, it will +scan for `@RedisHash` classes and configure Spring Data Redis repositories. Regular +`@Component` beans will not be loaded into the `ApplicationContext`: [source,java,indent=0] ---- @@ -6036,6 +6040,7 @@ A list of the auto-configuration that is enabled by `@DataRedisTest` can be <>. + [[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]] ==== Auto-configured Data LDAP tests `@DataLdapTest` can be used if you want to test LDAP applications. By default, it will diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index b36e860240..b56faa87b9 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -59,11 +59,6 @@ htmlunit true - - org.apache.commons - commons-pool2 - true - org.hibernate hibernate-core @@ -136,6 +131,11 @@ spring-data-neo4j true + + org.springframework.data + spring-data-redis + true + org.springframework.restdocs spring-restdocs-mockmvc @@ -151,16 +151,6 @@ spring-security-test true - - org.springframework.data - spring-data-redis - true - - - redis.clients - jedis - true - @@ -188,6 +178,11 @@ reactor-core test + + org.apache.commons + commons-pool2 + test + org.aspectj aspectjrt @@ -248,6 +243,11 @@ tomcat-embed-el test + + redis.clients + jedis + test + de.flapdoodle.embed de.flapdoodle.embed.mongo diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/AutoConfigureDataRedis.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/AutoConfigureDataRedis.java index 28904c614f..4001dbe8df 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/AutoConfigureDataRedis.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/AutoConfigureDataRedis.java @@ -31,7 +31,7 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; * than using this annotation directly. * * @author Jayaram Pradhan - * + * @since 2.0.0 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java index 1435454a31..22b55a7217 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java @@ -33,21 +33,18 @@ import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.core.annotation.AliasFor; import org.springframework.test.context.BootstrapWith; - - /** * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} * for a typical Data Redis test. Can be used when a test focuses only on - * RedisDB components. + * Redis components. *

* Using this annotation will disable full auto-configuration and instead apply only - * configuration relevant to RedisDB tests. + * configuration relevant to Redis tests. *

* * @author Jayaram Pradhan - * + * @since 2.0.0 */ - @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTypeExcludeFilter.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTypeExcludeFilter.java index 204d085205..b8585cf459 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTypeExcludeFilter.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTypeExcludeFilter.java @@ -24,8 +24,6 @@ import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizable import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.core.annotation.AnnotatedElementUtils; - - /** * {@link TypeExcludeFilter} for {@link DataRedisTest @DataRedisTest}. * @@ -71,4 +69,5 @@ class DataRedisTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter protected Set> getComponentIncludes() { return Collections.emptySet(); } + } diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 6d876eb370..e1727a9bd1 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -39,8 +39,7 @@ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration # AutoConfigureDataRedis auto-configuration imports org.springframework.boot.test.autoconfigure.data.redis.AutoConfigureDataRedis=\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ -org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ -org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration +org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration # AutoConfigureJdbc auto-configuration imports org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\ diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java index b20e37a0dd..32d3307997 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java @@ -25,6 +25,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.testsupport.rule.RedisTestServer; import org.springframework.context.ApplicationContext; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisOperations; @@ -32,7 +33,6 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; - /** * Integration test for {@link DataRedisTest}. * @@ -42,12 +42,15 @@ import static org.assertj.core.api.Assertions.assertThat; @DataRedisTest public class DataRedisTestIntegrationTests { - @Autowired - private RedisOperations operations; + @Rule + public RedisTestServer redis = new RedisTestServer(); @Rule public ExpectedException thrown = ExpectedException.none(); + @Autowired + private RedisOperations operations; + @Autowired private ExampleRepository exampleRepository; diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java index 86c73fc23e..13146cb7d7 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java @@ -16,10 +16,12 @@ package org.springframework.boot.test.autoconfigure.data.redis; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.testsupport.rule.RedisTestServer; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.stereotype.Service; import org.springframework.test.context.junit4.SpringRunner; @@ -35,6 +37,9 @@ import static org.assertj.core.api.Assertions.assertThat; @DataRedisTest(includeFilters = @Filter(Service.class)) public class DataRedisTestWithIncludeFilterIntegrationTests { + @Rule + public RedisTestServer redis = new RedisTestServer(); + @Autowired private ExampleRepository exampleRepository; @@ -48,7 +53,6 @@ public class DataRedisTestWithIncludeFilterIntegrationTests { assertThat(personHash.getId()).isNull(); PersonHash savedEntity = this.exampleRepository.save(personHash); assertThat(this.service.hasRecord(savedEntity)).isTrue(); - this.exampleRepository.deleteAll(); } } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java index e9588b4d3b..48246437e8 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java @@ -22,7 +22,6 @@ import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisOperations; import org.springframework.stereotype.Service; - /** * Example service used with {@link DataRedisTest} tests. * @@ -31,16 +30,17 @@ import org.springframework.stereotype.Service; @Service public class ExampleService { - private RedisOperations operations; private static final Charset CHARSET = Charset.forName("UTF-8"); + private RedisOperations operations; + public ExampleService(RedisOperations operations) { this.operations = operations; } public boolean hasRecord(PersonHash personHash) { - return this.operations.execute( - (RedisConnection connection) -> connection.exists(("persons:" + personHash.getId()).getBytes(CHARSET))); + return this.operations.execute((RedisConnection connection) -> + connection.exists(("persons:" + personHash.getId()).getBytes(CHARSET))); } } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/PersonHash.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/PersonHash.java index cb48da05e8..fe1e3b9949 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/PersonHash.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/PersonHash.java @@ -29,6 +29,7 @@ public class PersonHash { @Id private String id; + private String description; public String getId() { @@ -46,4 +47,5 @@ public class PersonHash { public void setDescription(String description) { this.description = description; } + }