Move MongoDB testcontainers support into spring-boot-mongodb

This commit is contained in:
Stéphane Nicoll
2025-05-19 16:18:14 +02:00
committed by Phillip Webb
parent 5c232d171a
commit 00a989d047
10 changed files with 12 additions and 10 deletions

View File

@@ -13,12 +13,15 @@ dependencies {
optional(project(":spring-boot-project:spring-boot-autoconfigure"))
optional(project(":spring-boot-project:spring-boot-reactor"))
optional(project(":spring-boot-project:spring-boot-testcontainers"))
optional("io.netty:netty-transport")
optional("org.mongodb:mongodb-driver-reactivestreams")
optional("org.mongodb:mongodb-driver-sync")
optional("org.testcontainers:mongodb")
testImplementation(project(":spring-boot-project:spring-boot-test"))
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation(testFixtures(project(":spring-boot-project:spring-boot-testcontainers")))
testRuntimeOnly("ch.qos.logback:logback-classic")
testRuntimeOnly("io.netty:netty-handler")

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2012-2025 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
*
* https://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.mongodb.testcontainers;
import com.mongodb.ConnectionString;
import org.testcontainers.containers.MongoDBContainer;
import org.springframework.boot.mongodb.autoconfigure.MongoConnectionDetails;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
/**
* {@link ContainerConnectionDetailsFactory} to create {@link MongoConnectionDetails} from
* a {@link ServiceConnection @ServiceConnection}-annotated {@link MongoDBContainer}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
class MongoContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<MongoDBContainer, MongoConnectionDetails> {
MongoContainerConnectionDetailsFactory() {
super(ANY_CONNECTION_NAME, "com.mongodb.ConnectionString");
}
@Override
protected MongoConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<MongoDBContainer> source) {
return new MongoContainerConnectionDetails(source);
}
/**
* {@link MongoConnectionDetails} backed by a {@link ContainerConnectionSource}.
*/
private static final class MongoContainerConnectionDetails extends ContainerConnectionDetails<MongoDBContainer>
implements MongoConnectionDetails {
private MongoContainerConnectionDetails(ContainerConnectionSource<MongoDBContainer> source) {
super(source);
}
@Override
public ConnectionString getConnectionString() {
return new ConnectionString(getContainer().getReplicaSetUrl());
}
@Override
public SslBundle getSslBundle() {
return super.getSslBundle();
}
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 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
*
* https://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.
*/
/**
* Support for testcontainers MongoDB service connections.
*/
package org.springframework.boot.mongodb.testcontainers;

View File

@@ -0,0 +1,3 @@
# Connection Details Factories
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
org.springframework.boot.mongodb.testcontainers.MongoContainerConnectionDetailsFactory

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2025 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
*
* https://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.mongodb.testcontainers;
import com.mongodb.ConnectionString;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactoryHints;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link MongoContainerConnectionDetailsFactory}.
*
* @author Moritz Halbritter
*/
class MongoContainerConnectionDetailsFactoryTests {
@Test
void shouldRegisterHints() {
RuntimeHints hints = ContainerConnectionDetailsFactoryHints.getRegisteredHints(getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection().onType(ConnectionString.class)).accepts(hints);
}
}