Skip to content

Commit f5262d8

Browse files
committed
Add missing Testcontainers service connection tests
Closes gh-35039
1 parent b9fcd57 commit f5262d8

10 files changed

+533
-0
lines changed

spring-boot-project/spring-boot-testcontainers/build.gradle

+14
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,34 @@ dependencies {
3636
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
3737
testImplementation(project(":spring-boot-project:spring-boot-test"))
3838
testImplementation("ch.qos.logback:logback-classic")
39+
testImplementation("co.elastic.clients:elasticsearch-java") {
40+
exclude group: "commons-logging", module: "commons-logging"
41+
}
42+
testImplementation("com.couchbase.client:java-client")
43+
testImplementation("com.datastax.oss:java-driver-core")
3944
testImplementation("org.assertj:assertj-core")
4045
testImplementation("org.awaitility:awaitility")
46+
testImplementation("org.flywaydb:flyway-core")
4147
testImplementation("org.influxdb:influxdb-java")
4248
testImplementation("org.junit.jupiter:junit-jupiter")
4349
testImplementation("org.junit.platform:junit-platform-engine")
4450
testImplementation("org.junit.platform:junit-platform-launcher")
51+
testImplementation("org.liquibase:liquibase-core") {
52+
exclude(group: "javax.xml.bind", module: "jaxb-api")
53+
}
4554
testImplementation("org.mockito:mockito-core")
4655
testImplementation("org.mockito:mockito-junit-jupiter")
4756
testImplementation("org.springframework:spring-core-test")
57+
testImplementation("org.springframework:spring-jdbc")
4858
testImplementation("org.springframework:spring-r2dbc")
4959
testImplementation("org.springframework.amqp:spring-rabbit")
60+
testImplementation("org.springframework.data:spring-data-redis")
5061
testImplementation("org.springframework.kafka:spring-kafka")
5162
testImplementation("org.testcontainers:junit-jupiter")
5263

5364
testRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc")
65+
testRuntimeOnly("com.zaxxer:HikariCP")
66+
testRuntimeOnly("io.lettuce:lettuce-core")
67+
testRuntimeOnly("org.postgresql:postgresql")
5468
}
5569

spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
3131
import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
3232
import org.springframework.boot.testcontainers.lifecycle.TestcontainersLifecycleApplicationContextInitializer;
33+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
3334
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
3435
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
3536
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -81,6 +82,7 @@ void whenHasExistingAutoConfigurationRegistersReplacement() {
8182
}
8283

8384
@Test
85+
@ClassPathExclusions("lettuce-core-*.jar")
8486
void whenHasUserConfigurationDoesNotRegisterReplacement() {
8587
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext()) {
8688
applicationContext.register(UserConfiguration.class, WithRedisAutoConfiguration.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2024 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+
17+
package org.springframework.boot.testcontainers.service.connection.cassandra;
18+
19+
import com.datastax.oss.driver.api.core.CqlSession;
20+
import org.junit.jupiter.api.Test;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.cassandra.CassandraConnectionDetails;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Tests for {@link CassandraContainerConnectionDetailsFactory}.
37+
*
38+
* @author Andy Wilkinson
39+
*/
40+
@SpringJUnitConfig
41+
@Testcontainers(disabledWithoutDocker = true)
42+
class CassandraContainerConnectionDetailsFactoryTests {
43+
44+
@Container
45+
@ServiceConnection
46+
static final CassandraContainer cassandra = new CassandraContainer();
47+
48+
@Autowired(required = false)
49+
private CassandraConnectionDetails connectionDetails;
50+
51+
@Autowired
52+
private CqlSession cqlSession;
53+
54+
@Test
55+
void connectionCanBeMadeToCassandraContainer() {
56+
assertThat(this.connectionDetails).isNotNull();
57+
assertThat(this.cqlSession.getMetadata().getNodes()).hasSize(1);
58+
}
59+
60+
@Configuration(proxyBeanMethods = false)
61+
@ImportAutoConfiguration(CassandraAutoConfiguration.class)
62+
static class TestConfiguration {
63+
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2024 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+
17+
package org.springframework.boot.testcontainers.service.connection.couchbase;
18+
19+
import java.time.Duration;
20+
21+
import com.couchbase.client.java.Cluster;
22+
import org.junit.jupiter.api.Test;
23+
import org.testcontainers.couchbase.BucketDefinition;
24+
import org.testcontainers.couchbase.CouchbaseContainer;
25+
import org.testcontainers.couchbase.CouchbaseService;
26+
import org.testcontainers.junit.jupiter.Container;
27+
import org.testcontainers.junit.jupiter.Testcontainers;
28+
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
31+
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration;
32+
import org.springframework.boot.autoconfigure.couchbase.CouchbaseConnectionDetails;
33+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
34+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
35+
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
40+
/**
41+
* Tests for {@link CouchbaseContainerConnectionDetailsFactory}.
42+
*
43+
* @author Andy Wilkinson
44+
*/
45+
@SpringJUnitConfig
46+
@Testcontainers(disabledWithoutDocker = true)
47+
class CouchbaseContainerConnectionDetailsFactoryTests {
48+
49+
@Container
50+
@ServiceConnection
51+
static final CouchbaseContainer couchbase = new CouchbaseContainer(DockerImageNames.couchbase())
52+
.withEnabledServices(CouchbaseService.KV, CouchbaseService.INDEX, CouchbaseService.QUERY)
53+
.withStartupAttempts(5)
54+
.withStartupTimeout(Duration.ofMinutes(10))
55+
.withBucket(new BucketDefinition("cbbucket"));
56+
57+
@Autowired(required = false)
58+
private CouchbaseConnectionDetails connectionDetails;
59+
60+
@Autowired
61+
private Cluster cluster;
62+
63+
@Test
64+
void connectionCanBeMadeToCouchbaseContainer() {
65+
assertThat(this.connectionDetails).isNotNull();
66+
assertThat(this.cluster.diagnostics().endpoints()).hasSize(1);
67+
}
68+
69+
@Configuration(proxyBeanMethods = false)
70+
@ImportAutoConfiguration(CouchbaseAutoConfiguration.class)
71+
static class TestConfiguration {
72+
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2024 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+
17+
package org.springframework.boot.testcontainers.service.connection.elasticsearch;
18+
19+
import java.io.IOException;
20+
import java.time.Duration;
21+
22+
import co.elastic.clients.elasticsearch.ElasticsearchClient;
23+
import org.junit.jupiter.api.Test;
24+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
25+
import org.testcontainers.junit.jupiter.Container;
26+
import org.testcontainers.junit.jupiter.Testcontainers;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientAutoConfiguration;
31+
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails;
32+
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
33+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
34+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
35+
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
40+
/**
41+
* Tests for {@link ElasticsearchContainerConnectionDetailsFactory}.
42+
*
43+
* @author Andy Wilkinson
44+
*/
45+
@SpringJUnitConfig
46+
@Testcontainers(disabledWithoutDocker = true)
47+
class ElasticsearchContainerConnectionDetailsFactoryTests {
48+
49+
@Container
50+
@ServiceConnection
51+
static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch())
52+
.withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m")
53+
.withStartupAttempts(5)
54+
.withStartupTimeout(Duration.ofMinutes(10));
55+
56+
@Autowired(required = false)
57+
private ElasticsearchConnectionDetails connectionDetails;
58+
59+
@Autowired
60+
private ElasticsearchClient client;
61+
62+
@Test
63+
void connectionCanBeMadeToElasticsearchContainer() throws IOException {
64+
assertThat(this.connectionDetails).isNotNull();
65+
assertThat(this.client.cluster().health().numberOfNodes()).isEqualTo(1);
66+
}
67+
68+
@Configuration(proxyBeanMethods = false)
69+
@ImportAutoConfiguration({ ElasticsearchClientAutoConfiguration.class,
70+
ElasticsearchRestClientAutoConfiguration.class })
71+
static class TestConfiguration {
72+
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2024 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+
17+
package org.springframework.boot.testcontainers.service.connection.flyway;
18+
19+
import org.flywaydb.core.Flyway;
20+
import org.junit.jupiter.api.Test;
21+
import org.testcontainers.containers.PostgreSQLContainer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
29+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
30+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.jdbc.core.JdbcTemplate;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.assertj.core.api.Assertions.assertThatNoException;
37+
38+
/**
39+
* Tests for {@link FlywayContainerConnectionDetailsFactory}.
40+
*
41+
* @author Andy Wilkinson
42+
*/
43+
@SpringJUnitConfig
44+
@Testcontainers(disabledWithoutDocker = true)
45+
class FlywayContainerConnectionDetailsFactoryTests {
46+
47+
@Container
48+
@ServiceConnection
49+
static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql());
50+
51+
@Autowired(required = false)
52+
private JdbcConnectionDetails connectionDetails;
53+
54+
@Autowired
55+
private Flyway flyway;
56+
57+
@Test
58+
void connectionCanBeMadeToJdbcContainer() {
59+
assertThat(this.connectionDetails).isNotNull();
60+
JdbcTemplate jdbc = new JdbcTemplate(this.flyway.getConfiguration().getDataSource());
61+
assertThatNoException().isThrownBy(() -> jdbc.execute("SELECT * from public.flyway_schema_history"));
62+
}
63+
64+
@Configuration(proxyBeanMethods = false)
65+
@ImportAutoConfiguration(FlywayAutoConfiguration.class)
66+
static class TestConfiguration {
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)