Skip to content

Commit dd846bd

Browse files
Switch Redis samples to Testcontainers.
1 parent 7f3d39d commit dd846bd

File tree

19 files changed

+106
-87
lines changed

19 files changed

+106
-87
lines changed

redis/example/pom.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
<dependencies>
1616

1717
<dependency>
18-
<groupId>${project.groupId}</groupId>
19-
<artifactId>spring-data-redis-example-utils</artifactId>
20-
<version>${project.version}</version>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-testcontainers</artifactId>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-test</artifactId>
2125
<scope>test</scope>
2226
</dependency>
2327

redis/example/src/test/java/example/springdata/redis/RedisTestConfiguration.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@
1515
*/
1616
package example.springdata.redis;
1717

18-
import jakarta.annotation.PreDestroy;
19-
20-
import org.springframework.beans.factory.annotation.Autowired;
18+
import com.redis.testcontainers.RedisContainer;
2119
import org.springframework.boot.autoconfigure.SpringBootApplication;
22-
import org.springframework.data.redis.connection.RedisConnectionFactory;
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
21+
import org.springframework.context.annotation.Bean;
22+
import org.testcontainers.utility.DockerImageName;
2323

2424
/**
2525
* @author Christoph Strobl
2626
*/
2727
@SpringBootApplication
2828
public class RedisTestConfiguration {
2929

30-
@Autowired RedisConnectionFactory factory;
31-
32-
/**
33-
* Clear database before shut down.
34-
*/
35-
public @PreDestroy void flushTestDb() {
36-
factory.getConnection().flushDb();
30+
@Bean
31+
@ServiceConnection(name = "redis")
32+
RedisContainer redisContainer() {
33+
return new RedisContainer(DockerImageName.parse("redis:7"));
3734
}
3835
}

redis/example/src/test/java/example/springdata/redis/commands/GeoOperationsTests.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
*/
1616
package example.springdata.redis.commands;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
20-
import example.springdata.redis.test.condition.EnabledOnCommand;
18+
import static org.assertj.core.api.Assertions.assertThat;
2119

2220
import org.junit.jupiter.api.BeforeEach;
2321
import org.junit.jupiter.api.Test;
24-
2522
import org.springframework.beans.factory.annotation.Autowired;
2623
import org.springframework.boot.test.context.SpringBootTest;
2724
import org.springframework.data.geo.Circle;
@@ -37,7 +34,6 @@
3734
* @author Mark Paluch
3835
*/
3936
@SpringBootTest
40-
@EnabledOnCommand("GEOADD")
4137
class GeoOperationsTests {
4238

4339
@Autowired RedisOperations<String, String> operations;

redis/example/src/test/java/example/springdata/redis/commands/KeyOperationsTests.java

-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
*/
1616
package example.springdata.redis.commands;
1717

18-
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
19-
2018
import java.util.UUID;
2119

2220
import org.junit.jupiter.api.BeforeEach;
2321
import org.junit.jupiter.api.Test;
24-
2522
import org.springframework.beans.factory.annotation.Autowired;
2623
import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;
2724
import org.springframework.data.redis.connection.RedisConnection;
@@ -36,7 +33,6 @@
3633
* @author Christoph Strobl
3734
*/
3835
@DataRedisTest
39-
@EnabledOnRedisAvailable
4036
class KeyOperationsTests {
4137

4238
private static final String PREFIX = KeyOperationsTests.class.getSimpleName();

redis/pom.xml

+12-12
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@
3333
<artifactId>spring-boot-starter-data-redis</artifactId>
3434
</dependency>
3535

36-
</dependencies>
37-
38-
<dependencyManagement>
39-
40-
<dependencies>
36+
<dependency>
37+
<groupId>org.testcontainers</groupId>
38+
<artifactId>junit-jupiter</artifactId>
39+
<scope>test</scope>
40+
</dependency>
4141

42-
<dependency>
43-
<groupId>com.github.kstyrc</groupId>
44-
<artifactId>embedded-redis</artifactId>
45-
<version>0.6</version>
46-
</dependency>
47-
</dependencies>
42+
<dependency>
43+
<groupId>com.redis</groupId>
44+
<artifactId>testcontainers-redis</artifactId>
45+
<version>2.2.2</version>
46+
<scope>test</scope>
47+
</dependency>
4848

49-
</dependencyManagement>
49+
</dependencies>
5050

5151
</project>

redis/pubsub/pom.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
<dependencies>
1717

1818
<dependency>
19-
<groupId>${project.groupId}</groupId>
20-
<artifactId>spring-data-redis-example-utils</artifactId>
21-
<version>${project.version}</version>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-testcontainers</artifactId>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-test</artifactId>
2226
<scope>test</scope>
2327
</dependency>
2428

redis/pubsub/src/test/java/example/springdata/redis/RedisTestConfiguration.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@
1515
*/
1616
package example.springdata.redis;
1717

18+
import com.redis.testcontainers.RedisContainer;
1819
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
21+
import org.springframework.context.annotation.Bean;
22+
import org.testcontainers.utility.DockerImageName;
1923

2024
/**
2125
* @author Mark Paluch
26+
* @author Christoph Strobl
2227
*/
2328
@SpringBootApplication
24-
public class RedisTestConfiguration {}
29+
public class RedisTestConfiguration {
30+
31+
@Bean
32+
@ServiceConnection(name = "redis")
33+
RedisContainer redisContainer() {
34+
return new RedisContainer(DockerImageName.parse("redis:7"));
35+
}
36+
}

redis/reactive/pom.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@
4141
</dependency>
4242

4343
<dependency>
44-
<groupId>${project.groupId}</groupId>
45-
<artifactId>spring-data-redis-example-utils</artifactId>
46-
<version>${project.version}</version>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-testcontainers</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-test</artifactId>
4751
<scope>test</scope>
4852
</dependency>
4953

redis/reactive/src/test/java/example/springdata/redis/RedisTestConfiguration.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@
1515
*/
1616
package example.springdata.redis;
1717

18-
import jakarta.annotation.PreDestroy;
19-
18+
import com.redis.testcontainers.RedisContainer;
2019
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
23-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
2423
import org.springframework.data.redis.core.ReactiveRedisTemplate;
2524
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
2625
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
2726
import org.springframework.data.redis.serializer.RedisSerializationContext;
2827
import org.springframework.data.redis.serializer.RedisSerializationContext.RedisSerializationContextBuilder;
2928
import org.springframework.data.redis.serializer.StringRedisSerializer;
29+
import org.testcontainers.utility.DockerImageName;
3030

3131
/**
3232
* @author Mark Paluch
33+
* @author Christoph Strobl
3334
*/
3435
@SpringBootApplication
3536
public class RedisTestConfiguration {
3637

3738
@Bean
38-
public LettuceConnectionFactory redisConnectionFactory() {
39-
return new LettuceConnectionFactory();
39+
@ServiceConnection(name = "redis")
40+
RedisContainer redisContainer() {
41+
return new RedisContainer(DockerImageName.parse("redis:7"));
4042
}
4143

4244
/**
@@ -72,10 +74,4 @@ public ReactiveRedisTemplate<String, Object> reactiveJsonObjectRedisTemplate(
7274
return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
7375
}
7476

75-
/**
76-
* Clear database before shut down.
77-
*/
78-
public @PreDestroy void flushTestDb() {
79-
redisConnectionFactory().getConnection().flushDb();
80-
}
8177
}

redis/reactive/src/test/java/example/springdata/redis/commands/KeyCommandsTests.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
*/
1616
package example.springdata.redis.commands;
1717

18-
import example.springdata.redis.RedisTestConfiguration;
19-
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
20-
import reactor.core.publisher.Flux;
21-
import reactor.test.StepVerifier;
22-
2318
import java.nio.ByteBuffer;
2419
import java.time.Duration;
2520
import java.util.Collections;
2621
import java.util.UUID;
2722

23+
import example.springdata.redis.RedisTestConfiguration;
2824
import org.junit.jupiter.api.BeforeEach;
2925
import org.junit.jupiter.api.Test;
30-
3126
import org.springframework.beans.factory.annotation.Autowired;
3227
import org.springframework.boot.test.context.SpringBootTest;
3328
import org.springframework.data.redis.connection.ReactiveRedisConnection;
@@ -36,6 +31,8 @@
3631
import org.springframework.data.redis.serializer.RedisSerializer;
3732
import org.springframework.data.redis.serializer.StringRedisSerializer;
3833
import org.springframework.data.redis.util.ByteUtils;
34+
import reactor.core.publisher.Flux;
35+
import reactor.test.StepVerifier;
3936

4037
/**
4138
* Show usage of reactive operations on Redis keys using low level API provided by
@@ -44,7 +41,6 @@
4441
* @author Mark Paluch
4542
*/
4643
@SpringBootTest(classes = RedisTestConfiguration.class)
47-
@EnabledOnRedisAvailable
4844
class KeyCommandsTests {
4945

5046
private static final String PREFIX = KeyCommandsTests.class.getSimpleName();

redis/reactive/src/test/java/example/springdata/redis/operations/JacksonJsonTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import example.springdata.redis.EmailAddress;
1919
import example.springdata.redis.Person;
2020
import example.springdata.redis.RedisTestConfiguration;
21-
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
2221
import lombok.extern.slf4j.Slf4j;
2322
import reactor.test.StepVerifier;
2423

@@ -39,7 +38,6 @@
3938
*/
4039
@Slf4j
4140
@SpringBootTest(classes = RedisTestConfiguration.class)
42-
@EnabledOnRedisAvailable
4341
class JacksonJsonTests {
4442

4543
@Autowired ReactiveRedisOperations<String, Person> typedOperations;

redis/reactive/src/test/java/example/springdata/redis/operations/ListOperationsTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package example.springdata.redis.operations;
1717

1818
import example.springdata.redis.RedisTestConfiguration;
19-
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
2019
import lombok.extern.slf4j.Slf4j;
2120
import reactor.core.publisher.Mono;
2221
import reactor.test.StepVerifier;
@@ -38,7 +37,6 @@
3837
*/
3938
@Slf4j
4039
@SpringBootTest(classes = RedisTestConfiguration.class)
41-
@EnabledOnRedisAvailable
4240
class ListOperationsTests {
4341

4442
@Autowired ReactiveRedisOperations<String, String> operations;

redis/reactive/src/test/java/example/springdata/redis/operations/ValueOperationsTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import example.springdata.redis.RedisTestConfiguration;
21-
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
2221
import lombok.extern.slf4j.Slf4j;
2322
import reactor.core.publisher.Mono;
2423
import reactor.test.StepVerifier;
@@ -39,7 +38,6 @@
3938
*/
4039
@Slf4j
4140
@SpringBootTest(classes = RedisTestConfiguration.class)
42-
@EnabledOnRedisAvailable
4341
class ValueOperationsTests {
4442

4543
@Autowired ReactiveRedisOperations<String, String> operations;

redis/repositories/pom.xml

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
<dependencies>
1616

1717
<dependency>
18-
<groupId>${project.groupId}</groupId>
19-
<artifactId>spring-data-redis-example-utils</artifactId>
20-
<version>${project.version}</version>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-testcontainers</artifactId>
2120
<scope>test</scope>
2221
</dependency>
23-
2422
<dependency>
25-
<groupId>com.github.kstyrc</groupId>
26-
<artifactId>embedded-redis</artifactId>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-test</artifactId>
2725
<scope>test</scope>
2826
</dependency>
2927

redis/repositories/src/test/java/example/springdata/redis/repositories/PersonRepositoryTests.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import example.springdata.redis.test.condition.EnabledOnRedisAvailable;
20+
import com.redis.testcontainers.RedisContainer;
2121

2222
import java.nio.charset.Charset;
2323
import java.nio.charset.StandardCharsets;
@@ -29,6 +29,7 @@
2929

3030
import org.springframework.beans.factory.annotation.Autowired;
3131
import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;
32+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
3233
import org.springframework.data.domain.Example;
3334
import org.springframework.data.domain.PageRequest;
3435
import org.springframework.data.domain.Pageable;
@@ -40,16 +41,23 @@
4041
import org.springframework.data.redis.core.RedisOperations;
4142
import org.springframework.data.redis.core.index.GeoIndexed;
4243
import org.springframework.data.redis.core.index.Indexed;
44+
import org.testcontainers.junit.jupiter.Container;
45+
import org.testcontainers.junit.jupiter.Testcontainers;
46+
import org.testcontainers.utility.DockerImageName;
4347

4448
/**
4549
* @author Christoph Strobl
4650
* @author Oliver Gierke
4751
* @author Mark Paluch
4852
*/
53+
@Testcontainers
4954
@DataRedisTest
50-
@EnabledOnRedisAvailable
5155
class PersonRepositoryTests {
5256

57+
@Container
58+
@ServiceConnection
59+
static RedisContainer redis = new RedisContainer(DockerImageName.parse("redis:7"));
60+
5361
/** {@link Charset} for String conversion **/
5462
private static final Charset CHARSET = StandardCharsets.UTF_8;
5563

redis/streams/pom.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
</dependency>
2323

2424
<dependency>
25-
<groupId>${project.groupId}</groupId>
26-
<artifactId>spring-data-redis-example-utils</artifactId>
27-
<version>${project.version}</version>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-testcontainers</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-test</artifactId>
2832
<scope>test</scope>
2933
</dependency>
3034

0 commit comments

Comments
 (0)