From 2d36d2a7e4541bfbb16534a048f0aa78c2a81636 Mon Sep 17 00:00:00 2001 From: Jayaram Pradhan Date: Sun, 14 May 2017 07:51:20 +0530 Subject: [PATCH 1/2] Add Slice test annotation for Redis This commit adds new annotation `@DataRedisTest` which provides test infrastructure for redis. See gh-9224 --- .../main/asciidoc/spring-boot-features.adoc | 24 +++++ spring-boot-test-autoconfigure/pom.xml | 16 ++++ .../data/redis/AutoConfigureDataRedis.java | 43 +++++++++ .../data/redis/DataRedisTest.java | 94 +++++++++++++++++++ .../redis/DataRedisTypeExcludeFilter.java | 74 +++++++++++++++ .../main/resources/META-INF/spring.factories | 8 +- .../redis/DataRedisTestIntegrationTests.java | 76 +++++++++++++++ ...TestWithIncludeFilterIntegrationTests.java | 54 +++++++++++ .../data/redis/ExampleRedisApplication.java | 29 ++++++ .../data/redis/ExampleRepository.java | 27 ++++++ .../data/redis/ExampleService.java | 46 +++++++++ .../autoconfigure/data/redis/PersonHash.java | 49 ++++++++++ 12 files changed, 539 insertions(+), 1 deletion(-) create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/AutoConfigureDataRedis.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTypeExcludeFilter.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRedisApplication.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRepository.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/PersonHash.java 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 c002ab95900..eb6c5b9ca2c 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -6010,6 +6010,30 @@ 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. + +[source,java,indent=0] +---- + import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest; + import org.springframework.test.context.junit4.SpringRunner; + + @RunWith(SpringRunner.class) + @DataRedisTest + public class ExampleDataRedisTests { + + @Autowired + private YourRepository repository; + + // + } +---- + +A list of the auto-configuration that is enabled by `@DataRedisTest` can be +<>. [[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]] diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index e47f4e9d6e6..b36e860240f 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -59,6 +59,11 @@ htmlunit true + + org.apache.commons + commons-pool2 + true + org.hibernate hibernate-core @@ -146,6 +151,17 @@ spring-security-test true + + org.springframework.data + spring-data-redis + true + + + redis.clients + jedis + true + + org.springframework.boot 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 new file mode 100644 index 00000000000..28904c614f2 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/AutoConfigureDataRedis.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; + +/** + * {@link ImportAutoConfiguration Auto-configuration imports} for typical Data redis + * tests. Most tests should consider using {@link DataRedisTest @DataRedisTest} rather + * than using this annotation directly. + * + * @author Jayaram Pradhan + * + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ImportAutoConfiguration +public @interface AutoConfigureDataRedis { + +} 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 new file mode 100644 index 00000000000..1435454a31e --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; +import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +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. + *

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

+ * + * @author Jayaram Pradhan + * + */ + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@BootstrapWith(SpringBootTestContextBootstrapper.class) +@OverrideAutoConfiguration(enabled = false) +@TypeExcludeFilters(DataRedisTypeExcludeFilter.class) +@AutoConfigureCache +@AutoConfigureDataRedis +@ImportAutoConfiguration +public @interface DataRedisTest { + + /** + * Determines if default filtering should be used with + * {@link SpringBootApplication @SpringBootApplication}. By default no beans are + * included. + * @see #includeFilters() + * @see #excludeFilters() + * @return if default filters should be used + */ + boolean useDefaultFilters() default true; + + /** + * A set of include filters which can be used to add otherwise filtered beans to the + * application context. + * @return include filters to apply + */ + Filter[] includeFilters() default {}; + + /** + * A set of exclude filters which can be used to filter beans that would otherwise be + * added to the application context. + * @return exclude filters to apply + */ + Filter[] excludeFilters() default {}; + + /** + * Auto-configuration exclusions that should be applied for this test. + * @return auto-configuration exclusions to apply + */ + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") + Class[] excludeAutoConfiguration() default {}; + +} 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 new file mode 100644 index 00000000000..204d0852057 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTypeExcludeFilter.java @@ -0,0 +1,74 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import java.util.Collections; +import java.util.Set; + +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.core.annotation.AnnotatedElementUtils; + + + +/** + * {@link TypeExcludeFilter} for {@link DataRedisTest @DataRedisTest}. + * + * @author Jayaram Pradhan + */ +class DataRedisTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { + + private final DataRedisTest annotation; + + DataRedisTypeExcludeFilter(Class testClass) { + this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, + DataRedisTest.class); + } + + @Override + protected boolean hasAnnotation() { + return this.annotation != null; + } + + @Override + protected Filter[] getFilters(FilterType type) { + switch (type) { + case INCLUDE: + return this.annotation.includeFilters(); + case EXCLUDE: + return this.annotation.excludeFilters(); + default: + throw new IllegalStateException("Unsupported type " + type); + } + } + + @Override + protected boolean isUseDefaultFilters() { + return this.annotation.useDefaultFilters(); + } + + @Override + protected Set> getDefaultIncludes() { + return Collections.emptySet(); + } + + @Override + 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 44963011442..6d876eb3700 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 @@ -36,6 +36,12 @@ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ 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 + # AutoConfigureJdbc auto-configuration imports org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ @@ -136,4 +142,4 @@ org.springframework.test.context.TestExecutionListener=\ org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\ -org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener +org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener \ No newline at end of file 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 new file mode 100644 index 00000000000..b20e37a0ddf --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import java.nio.charset.Charset; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * Integration test for {@link DataRedisTest}. + * + * @author Jayaram Pradhan + */ +@RunWith(SpringRunner.class) +@DataRedisTest +public class DataRedisTestIntegrationTests { + + @Autowired + private RedisOperations operations; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Autowired + private ExampleRepository exampleRepository; + + @Autowired + private ApplicationContext applicationContext; + + private static final Charset CHARSET = Charset.forName("UTF-8"); + + @Test + public void testRepository() { + PersonHash personHash = new PersonHash(); + personHash.setDescription("Look, new @DataRedisTest!"); + assertThat(personHash.getId()).isNull(); + PersonHash savedEntity = this.exampleRepository.save(personHash); + assertThat(savedEntity.getId()).isNotNull(); + assertThat(this.operations.execute((RedisConnection connection) -> connection.exists(("persons:" + savedEntity.getId()).getBytes(CHARSET)))).isTrue(); + this.exampleRepository.deleteAll(); + } + + @Test + public void didNotInjectExampleService() { + this.thrown.expect(NoSuchBeanDefinitionException.class); + this.applicationContext.getBean(ExampleService.class); + } + +} 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 new file mode 100644 index 00000000000..86c73fc23e8 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.stereotype.Service; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration test with custom include filter for {@link DataRedisTest}. + * + * @author Jayaram Pradhan + */ +@RunWith(SpringRunner.class) +@DataRedisTest(includeFilters = @Filter(Service.class)) +public class DataRedisTestWithIncludeFilterIntegrationTests { + + @Autowired + private ExampleRepository exampleRepository; + + @Autowired + private ExampleService service; + + @Test + public void testService() { + PersonHash personHash = new PersonHash(); + personHash.setDescription("Look, new @DataRedisTest!"); + 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/ExampleRedisApplication.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRedisApplication.java new file mode 100644 index 00000000000..6afd3f45e86 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRedisApplication.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Example {@link SpringBootApplication} used with {@link DataRedisTest} tests. + * + * @author Jayaram Pradhan + */ +@SpringBootApplication +public class ExampleRedisApplication { + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRepository.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRepository.java new file mode 100644 index 00000000000..b8c65493620 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import org.springframework.data.repository.CrudRepository; + +/** + * Example repository used with {@link DataRedisTest} tests. + * + * @author Jayaram Pradhan + */ +public interface ExampleRepository extends CrudRepository { +} 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 new file mode 100644 index 00000000000..e9588b4d3b8 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import java.nio.charset.Charset; + +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. + * + * @author Jayaram Pradhan + */ +@Service +public class ExampleService { + + private RedisOperations operations; + private static final Charset CHARSET = Charset.forName("UTF-8"); + + 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))); + } + +} 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 new file mode 100644 index 00000000000..cb48da05e88 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/PersonHash.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.redis; + +import org.springframework.data.annotation.Id; +import org.springframework.data.redis.core.RedisHash; + +/** + * Example graph used with {@link DataRedisTest} tests. + * + * @author Jayaram Pradhan + */ +@RedisHash("persons") +public class PersonHash { + + @Id + private String id; + private String description; + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } +} From dd53ed0aecc5e986ab758490ff160fc8b858318d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 5 Jun 2017 10:55:40 +0200 Subject: [PATCH 2/2] 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 eb6c5b9ca2c..03206604bc8 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 b36e860240f..b56faa87b97 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 28904c614f2..4001dbe8dfd 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 1435454a31e..22b55a72173 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 204d0852057..b8585cf459f 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 6d876eb3700..e1727a9bd11 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 b20e37a0ddf..32d3307997f 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 86c73fc23e8..13146cb7d78 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 e9588b4d3b8..48246437e8a 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 cb48da05e88..fe1e3b99494 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; } + }