Skip to content

Commit a965117

Browse files
quaffrwinch
authored andcommitted
Polish usage of spring-boot-testcontainers
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 02ab97d commit a965117

File tree

19 files changed

+98
-108
lines changed

19 files changed

+98
-108
lines changed

spring-session-samples/spring-session-sample-boot-findbyusername/spring-session-sample-boot-findbyusername.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ dependencies {
2222

2323
integrationTestCompile "org.htmlunit:htmlunit"
2424
integrationTestCompile "org.seleniumhq.selenium:htmlunit3-driver"
25-
integrationTestCompile "org.testcontainers:testcontainers"
25+
integrationTestCompile "org.springframework.boot:spring-boot-testcontainers"
26+
integrationTestCompile "com.redis:testcontainers-redis"
2627
}

spring-session-samples/spring-session-sample-boot-findbyusername/src/integration-test/java/sample/FindByUsernameTests.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,12 @@
1616

1717
package sample;
1818

19+
import com.redis.testcontainers.RedisContainer;
1920
import org.junit.jupiter.api.AfterEach;
2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.extension.ExtendWith;
2324
import org.openqa.selenium.WebDriver;
24-
import org.testcontainers.containers.GenericContainer;
2525
import sample.pages.HomePage;
2626
import sample.pages.LoginPage;
2727

@@ -30,8 +30,8 @@
3030
import org.springframework.boot.test.context.SpringBootTest;
3131
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3232
import org.springframework.boot.test.context.TestConfiguration;
33+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
3334
import org.springframework.context.annotation.Bean;
34-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
3535
import org.springframework.test.context.junit.jupiter.SpringExtension;
3636
import org.springframework.test.web.servlet.MockMvc;
3737
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
@@ -40,6 +40,7 @@
4040
* @author Eddú Meléndez
4141
* @author Rob Winch
4242
* @author Vedran Pavic
43+
* @author Yanming Zhou
4344
*/
4445
@ExtendWith(SpringExtension.class)
4546
@AutoConfigureMockMvc
@@ -107,15 +108,9 @@ private static HomePage home(WebDriver driver) {
107108
static class Config {
108109

109110
@Bean
110-
GenericContainer redisContainer() {
111-
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE).withExposedPorts(6379);
112-
redisContainer.start();
113-
return redisContainer;
114-
}
115-
116-
@Bean
117-
LettuceConnectionFactory redisConnectionFactory() {
118-
return new LettuceConnectionFactory(redisContainer().getHost(), redisContainer().getFirstMappedPort());
111+
@ServiceConnection
112+
RedisContainer redisContainer() {
113+
return new RedisContainer(DOCKER_IMAGE);
119114
}
120115

121116
}

spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/test/java/sample/JdbcJsonAttributeTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2014-2025 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+
117
package sample;
218

319
import java.util.Base64;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2014-2025 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+
117
package sample;
218

319
import org.testcontainers.containers.PostgreSQLContainer;
@@ -10,17 +26,10 @@
1026
@TestConfiguration(proxyBeanMethods = false)
1127
public class TestContainersConfig {
1228

13-
static PostgreSQLContainer<?> postgresql = new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.1"))
14-
.withExposedPorts(5432);
15-
16-
static {
17-
postgresql.start();
18-
}
19-
2029
@Bean
2130
@ServiceConnection
2231
PostgreSQLContainer<?> postgresContainer() {
23-
return postgresql;
32+
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.1"));
2433
}
2534

2635
}

spring-session-samples/spring-session-sample-boot-reactive-max-sessions/spring-session-sample-boot-reactive-max-sessions.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies {
1313
testImplementation 'io.projectreactor:reactor-test'
1414
testImplementation 'org.springframework.security:spring-security-test'
1515
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
16-
testImplementation 'org.testcontainers:junit-jupiter'
16+
testImplementation 'com.redis:testcontainers-redis'
1717
testImplementation "org.htmlunit:htmlunit"
1818
testImplementation "org.seleniumhq.selenium:htmlunit3-driver"
1919

spring-session-samples/spring-session-sample-boot-reactive-max-sessions/src/test/java/com/example/TestcontainersConfig.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2024 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package com.example;
1818

19-
import org.testcontainers.containers.GenericContainer;
19+
import com.redis.testcontainers.RedisContainer;
2020
import org.testcontainers.utility.DockerImageName;
2121

2222
import org.springframework.boot.test.context.TestConfiguration;
@@ -27,9 +27,9 @@
2727
public class TestcontainersConfig {
2828

2929
@Bean
30-
@ServiceConnection(name = "redis")
31-
GenericContainer<?> redisContainer() {
32-
return new GenericContainer<>(DockerImageName.parse("redis:6.2.6")).withExposedPorts(6379);
30+
@ServiceConnection
31+
RedisContainer redisContainer() {
32+
return new RedisContainer(DockerImageName.parse("redis:6.2.6"));
3333
}
3434

3535
}

spring-session-samples/spring-session-sample-boot-reactive-redis-indexed/spring-session-sample-boot-reactive-redis-indexed.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
testImplementation 'io.projectreactor:reactor-test'
1212
testImplementation 'org.springframework.security:spring-security-test'
1313
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
14-
testImplementation 'org.testcontainers:junit-jupiter'
14+
testImplementation 'com.redis:testcontainers-redis'
1515
testImplementation "org.htmlunit:htmlunit"
1616
testImplementation "org.seleniumhq.selenium:htmlunit3-driver"
1717

spring-session-samples/spring-session-sample-boot-reactive-redis-indexed/src/test/java/com/example/TestcontainersConfig.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package com.example;
1818

19-
import org.testcontainers.containers.GenericContainer;
19+
import com.redis.testcontainers.RedisContainer;
2020
import org.testcontainers.utility.DockerImageName;
2121

2222
import org.springframework.boot.test.context.TestConfiguration;
@@ -27,9 +27,9 @@
2727
public class TestcontainersConfig {
2828

2929
@Bean
30-
@ServiceConnection(name = "redis")
31-
GenericContainer<?> redisContainer() {
32-
return new GenericContainer<>(DockerImageName.parse("redis:6.2.6")).withExposedPorts(6379);
30+
@ServiceConnection
31+
RedisContainer redisContainer() {
32+
return new RedisContainer(DockerImageName.parse("redis:6.2.6"));
3333
}
3434

3535
}

spring-session-samples/spring-session-sample-boot-redis-json/spring-session-sample-boot-redis-json.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ dependencies {
2323

2424
integrationTestCompile "org.htmlunit:htmlunit"
2525
integrationTestCompile "org.seleniumhq.selenium:htmlunit3-driver"
26-
integrationTestCompile "org.testcontainers:testcontainers"
26+
integrationTestCompile "org.springframework.boot:spring-boot-testcontainers"
27+
integrationTestCompile "com.redis:testcontainers-redis"
2728
}

spring-session-samples/spring-session-sample-boot-redis-json/src/integration-test/java/sample/HttpRedisJsonTest.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import java.util.List;
2020

21+
import com.redis.testcontainers.RedisContainer;
2122
import org.junit.jupiter.api.AfterEach;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425
import org.junit.jupiter.api.extension.ExtendWith;
2526
import org.openqa.selenium.WebDriver;
26-
import org.testcontainers.containers.GenericContainer;
2727
import sample.pages.HomePage;
2828
import sample.pages.HomePage.Attribute;
2929
import sample.pages.LoginPage;
@@ -34,7 +34,7 @@
3434
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3535
import org.springframework.boot.test.context.TestConfiguration;
3636
import org.springframework.context.annotation.Bean;
37-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
37+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
3838
import org.springframework.test.context.junit.jupiter.SpringExtension;
3939
import org.springframework.test.web.servlet.MockMvc;
4040
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
@@ -107,15 +107,9 @@ void createAttribute() {
107107
static class Config {
108108

109109
@Bean
110-
GenericContainer redisContainer() {
111-
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE).withExposedPorts(6379);
112-
redisContainer.start();
113-
return redisContainer;
114-
}
115-
116-
@Bean
117-
LettuceConnectionFactory redisConnectionFactory() {
118-
return new LettuceConnectionFactory(redisContainer().getHost(), redisContainer().getFirstMappedPort());
110+
@ServiceConnection
111+
RedisContainer redisContainer() {
112+
return new RedisContainer(DOCKER_IMAGE);
119113
}
120114

121115
}

spring-session-samples/spring-session-sample-boot-redis-json/src/integration-test/java/sample/RedisSerializerTest.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
package sample;
1818

19+
import com.redis.testcontainers.RedisContainer;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.extension.ExtendWith;
21-
import org.testcontainers.containers.GenericContainer;
2222

2323
import org.springframework.boot.test.context.SpringBootTest;
2424
import org.springframework.boot.test.context.TestConfiguration;
2525
import org.springframework.context.annotation.Bean;
26-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
26+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
2727
import org.springframework.data.redis.core.RedisTemplate;
2828
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
2929
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisOperations;
@@ -56,15 +56,9 @@ void testRedisTemplate() {
5656
static class Config {
5757

5858
@Bean
59-
GenericContainer redisContainer() {
60-
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE).withExposedPorts(6379);
61-
redisContainer.start();
62-
return redisContainer;
63-
}
64-
65-
@Bean
66-
LettuceConnectionFactory redisConnectionFactory() {
67-
return new LettuceConnectionFactory(redisContainer().getHost(), redisContainer().getFirstMappedPort());
59+
@ServiceConnection
60+
RedisContainer redisContainer() {
61+
return new RedisContainer(DOCKER_IMAGE);
6862
}
6963

7064
}

spring-session-samples/spring-session-sample-boot-redis/spring-session-sample-boot-redis.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ dependencies {
2222
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
2323
integrationTestCompile "org.htmlunit:htmlunit"
2424
integrationTestCompile "org.seleniumhq.selenium:htmlunit3-driver"
25-
integrationTestCompile "org.testcontainers:testcontainers"
25+
integrationTestCompile "org.springframework.boot:spring-boot-testcontainers"
26+
integrationTestCompile "com.redis:testcontainers-redis"
2627
}

spring-session-samples/spring-session-sample-boot-redis/src/integration-test/java/sample/BootTests.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,12 @@
1616

1717
package sample;
1818

19+
import com.redis.testcontainers.RedisContainer;
1920
import org.junit.jupiter.api.AfterEach;
2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.extension.ExtendWith;
2324
import org.openqa.selenium.WebDriver;
24-
import org.testcontainers.containers.GenericContainer;
2525
import sample.pages.HomePage;
2626
import sample.pages.LoginPage;
2727

@@ -30,8 +30,8 @@
3030
import org.springframework.boot.test.context.SpringBootTest;
3131
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3232
import org.springframework.boot.test.context.TestConfiguration;
33+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
3334
import org.springframework.context.annotation.Bean;
34-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
3535
import org.springframework.test.context.junit.jupiter.SpringExtension;
3636
import org.springframework.test.web.servlet.MockMvc;
3737
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
@@ -89,15 +89,9 @@ void logout() {
8989
static class Config {
9090

9191
@Bean
92-
GenericContainer redisContainer() {
93-
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE).withExposedPorts(6379);
94-
redisContainer.start();
95-
return redisContainer;
96-
}
97-
98-
@Bean
99-
LettuceConnectionFactory redisConnectionFactory() {
100-
return new LettuceConnectionFactory(redisContainer().getHost(), redisContainer().getFirstMappedPort());
92+
@ServiceConnection
93+
RedisContainer redisContainer() {
94+
return new RedisContainer(DOCKER_IMAGE);
10195
}
10296

10397
}

spring-session-samples/spring-session-sample-boot-webflux-custom-cookie/spring-session-sample-boot-webflux-custom-cookie.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ dependencies {
1717

1818
integrationTestCompile "org.htmlunit:htmlunit"
1919
integrationTestCompile "org.seleniumhq.selenium:htmlunit3-driver"
20-
integrationTestCompile "org.testcontainers:testcontainers"
20+
integrationTestCompile "org.springframework.boot:spring-boot-testcontainers"
21+
integrationTestCompile "com.redis:testcontainers-redis"
2122
}

0 commit comments

Comments
 (0)