Commit dd53ed0a authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Add Slice test annotation for Redis"

Closes gh-9224
parent 2d36d2a7
...@@ -6010,9 +6010,13 @@ disable transaction management for a test or for the whole class as follows: ...@@ -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 A list of the auto-configuration that is enabled by `@DataNeo4jTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>. <<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-redis-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-redis-test]]
==== Auto-configured Data Redis tests ==== 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] [source,java,indent=0]
---- ----
...@@ -6036,6 +6040,7 @@ A list of the auto-configuration that is enabled by `@DataRedisTest` can be ...@@ -6036,6 +6040,7 @@ A list of the auto-configuration that is enabled by `@DataRedisTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>. <<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]]
==== Auto-configured Data LDAP tests ==== Auto-configured Data LDAP tests
`@DataLdapTest` can be used if you want to test LDAP applications. By default, it will `@DataLdapTest` can be used if you want to test LDAP applications. By default, it will
......
...@@ -59,11 +59,6 @@ ...@@ -59,11 +59,6 @@
<artifactId>htmlunit</artifactId> <artifactId>htmlunit</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId> <artifactId>hibernate-core</artifactId>
...@@ -136,6 +131,11 @@ ...@@ -136,6 +131,11 @@
<artifactId>spring-data-neo4j</artifactId> <artifactId>spring-data-neo4j</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.restdocs</groupId> <groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId> <artifactId>spring-restdocs-mockmvc</artifactId>
...@@ -151,16 +151,6 @@ ...@@ -151,16 +151,6 @@
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<optional>true</optional>
</dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
...@@ -188,6 +178,11 @@ ...@@ -188,6 +178,11 @@
<artifactId>reactor-core</artifactId> <artifactId>reactor-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.aspectj</groupId> <groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId> <artifactId>aspectjrt</artifactId>
...@@ -248,6 +243,11 @@ ...@@ -248,6 +243,11 @@
<artifactId>tomcat-embed-el</artifactId> <artifactId>tomcat-embed-el</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>de.flapdoodle.embed</groupId> <groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId> <artifactId>de.flapdoodle.embed.mongo</artifactId>
......
...@@ -31,7 +31,7 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; ...@@ -31,7 +31,7 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
* than using this annotation directly. * than using this annotation directly.
* *
* @author Jayaram Pradhan * @author Jayaram Pradhan
* * @since 2.0.0
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
......
...@@ -33,21 +33,18 @@ import org.springframework.context.annotation.ComponentScan.Filter; ...@@ -33,21 +33,18 @@ import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor; import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.BootstrapWith; import org.springframework.test.context.BootstrapWith;
/** /**
* Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} * 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 <strong>only</strong> on * for a typical Data Redis test. Can be used when a test focuses <strong>only</strong> on
* RedisDB components. * Redis components.
* <p> * <p>
* Using this annotation will disable full auto-configuration and instead apply only * Using this annotation will disable full auto-configuration and instead apply only
* configuration relevant to RedisDB tests. * configuration relevant to Redis tests.
* <p> * <p>
* *
* @author Jayaram Pradhan * @author Jayaram Pradhan
* * @since 2.0.0
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
......
...@@ -24,8 +24,6 @@ import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizable ...@@ -24,8 +24,6 @@ import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizable
import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotatedElementUtils;
/** /**
* {@link TypeExcludeFilter} for {@link DataRedisTest @DataRedisTest}. * {@link TypeExcludeFilter} for {@link DataRedisTest @DataRedisTest}.
* *
...@@ -71,4 +69,5 @@ class DataRedisTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter ...@@ -71,4 +69,5 @@ class DataRedisTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter
protected Set<Class<?>> getComponentIncludes() { protected Set<Class<?>> getComponentIncludes() {
return Collections.emptySet(); return Collections.emptySet();
} }
} }
...@@ -39,8 +39,7 @@ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration ...@@ -39,8 +39,7 @@ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
# AutoConfigureDataRedis auto-configuration imports # AutoConfigureDataRedis auto-configuration imports
org.springframework.boot.test.autoconfigure.data.redis.AutoConfigureDataRedis=\ org.springframework.boot.test.autoconfigure.data.redis.AutoConfigureDataRedis=\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
# AutoConfigureJdbc auto-configuration imports # AutoConfigureJdbc auto-configuration imports
org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\ org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\
......
...@@ -25,6 +25,7 @@ import org.junit.runner.RunWith; ...@@ -25,6 +25,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testsupport.rule.RedisTestServer;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisOperations;
...@@ -32,7 +33,6 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -32,7 +33,6 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Integration test for {@link DataRedisTest}. * Integration test for {@link DataRedisTest}.
* *
...@@ -42,12 +42,15 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -42,12 +42,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DataRedisTest @DataRedisTest
public class DataRedisTestIntegrationTests { public class DataRedisTestIntegrationTests {
@Autowired @Rule
private RedisOperations<Object, Object> operations; public RedisTestServer redis = new RedisTestServer();
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Autowired
private RedisOperations<Object, Object> operations;
@Autowired @Autowired
private ExampleRepository exampleRepository; private ExampleRepository exampleRepository;
......
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
package org.springframework.boot.test.autoconfigure.data.redis; package org.springframework.boot.test.autoconfigure.data.redis;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testsupport.rule.RedisTestServer;
import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
...@@ -35,6 +37,9 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -35,6 +37,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DataRedisTest(includeFilters = @Filter(Service.class)) @DataRedisTest(includeFilters = @Filter(Service.class))
public class DataRedisTestWithIncludeFilterIntegrationTests { public class DataRedisTestWithIncludeFilterIntegrationTests {
@Rule
public RedisTestServer redis = new RedisTestServer();
@Autowired @Autowired
private ExampleRepository exampleRepository; private ExampleRepository exampleRepository;
...@@ -48,7 +53,6 @@ public class DataRedisTestWithIncludeFilterIntegrationTests { ...@@ -48,7 +53,6 @@ public class DataRedisTestWithIncludeFilterIntegrationTests {
assertThat(personHash.getId()).isNull(); assertThat(personHash.getId()).isNull();
PersonHash savedEntity = this.exampleRepository.save(personHash); PersonHash savedEntity = this.exampleRepository.save(personHash);
assertThat(this.service.hasRecord(savedEntity)).isTrue(); assertThat(this.service.hasRecord(savedEntity)).isTrue();
this.exampleRepository.deleteAll();
} }
} }
...@@ -22,7 +22,6 @@ import org.springframework.data.redis.connection.RedisConnection; ...@@ -22,7 +22,6 @@ import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* Example service used with {@link DataRedisTest} tests. * Example service used with {@link DataRedisTest} tests.
* *
...@@ -31,16 +30,17 @@ import org.springframework.stereotype.Service; ...@@ -31,16 +30,17 @@ import org.springframework.stereotype.Service;
@Service @Service
public class ExampleService { public class ExampleService {
private RedisOperations<Object, Object> operations;
private static final Charset CHARSET = Charset.forName("UTF-8"); private static final Charset CHARSET = Charset.forName("UTF-8");
private RedisOperations<Object, Object> operations;
public ExampleService(RedisOperations<Object, Object> operations) { public ExampleService(RedisOperations<Object, Object> operations) {
this.operations = operations; this.operations = operations;
} }
public boolean hasRecord(PersonHash personHash) { public boolean hasRecord(PersonHash personHash) {
return this.operations.execute( return this.operations.execute((RedisConnection connection) ->
(RedisConnection connection) -> connection.exists(("persons:" + personHash.getId()).getBytes(CHARSET))); connection.exists(("persons:" + personHash.getId()).getBytes(CHARSET)));
} }
} }
...@@ -29,6 +29,7 @@ public class PersonHash { ...@@ -29,6 +29,7 @@ public class PersonHash {
@Id @Id
private String id; private String id;
private String description; private String description;
public String getId() { public String getId() {
...@@ -46,4 +47,5 @@ public class PersonHash { ...@@ -46,4 +47,5 @@ public class PersonHash {
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment