Test support Testcontainers MongoDB
Overview
Contains support for using a MongoDB testcontainer in integration tests. Provides extension methods that wire up a MongoDbContainer instance to either another testcontainer's builder or an IConfigurationBuilder, so that the containerised MongoDB is automatically connected to the component under test.
Install
- ASOL.Core.QA.TestSupport.Testcontainers.MongoDb
WithMongoDb (IContainerBuilder)
Use ContainerBuilderExtensions.WithMongoDb to wire a MongoDB testcontainer into another testcontainer that acts as a test tool — for example, a service container spun up as a dependency to support a test. The method injects the MongoDB connection details as environment variables into that testcontainer and declares a startup dependency so the MongoDB container is started first.
Note: The MongoDB test container does not need to be started before calling this method. The dependency is resolved automatically when the target container starts.
Signature
public static TBuilderEntity WithMongoDb<TBuilderEntity, TContainerEntity, TConfigurationEntity>(
this IContainerBuilder<TBuilderEntity, TContainerEntity, TConfigurationEntity> builder,
MongoDbContainer mongoDbContainer,
string sectionName,
PartialMongoDbConnectionOptions? options = null)
| Parameter | Description |
|---|---|
builder | The IContainerBuilder for the testcontainer acting as a test tool (e.g. a service container used as a test dependency, not the SUT). |
mongoDbContainer | The MongoDbContainer instance to connect to. The container must have a name or a network alias so it can be resolved by the target container over the shared Docker network. |
sectionName | The configuration section name used by the application to read MongoDB options (e.g. "MasterConnectionOptions"). |
options | Optional additional connection options. See PartialMongoDbConnectionOptions. |
Example
var network = new NetworkBuilder().Build();
var mongoDbContainer = new MongoDbBuilder("mongo:7.0")
.WithName($"test-mongodb-{Guid.NewGuid():N}")
.WithNetwork(network)
.Build();
var testApiContainerBuilder = new ContainerBuilder(testApiImage)
.WithNetwork(network)
.WithPortBinding(80, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilInternalTcpPortIsAvailable(80))
.WithMongoDb(mongoDbContainer, "MasterConnectionOptions");
var testAppContainer = testApiContainerBuilder.Build();
await testAppContainer.StartAsync(cancellationToken);
AddMongoDb (IConfigurationBuilder)
Use ConfigurationBuilderExtensions.AddMongoDb when the application under test uses standard Core MongoDb connection patter. The method reads the connection string from the already-running MongoDB testcontainer and adds it to the IConfigurationBuilder.
Note: The MongoDB test container must be started before calling this method.
Signature
public static IConfigurationBuilder AddMongoDb(
this IConfigurationBuilder configurationBuilder,
MongoDbContainer mongoDbContainer,
string sectionName,
PartialMongoDbConnectionOptions? options = null)
| Parameter | Description |
|---|---|
configurationBuilder | The configuration builder to add MongoDB settings to. |
mongoDbContainer | A started MongoDbContainer instance. |
sectionName | The configuration section name (e.g. "MasterConnectionOptions"). |
options | Optional additional connection options. See PartialMongoDbConnectionOptions. |
Example
// In a WebApplicationFactory override
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
config.AddMongoDb(
mongoDbContainer,
sectionName: "MasterConnectionOptions",
options: new PartialMongoDbConnectionOptions
{
DatabaseName = "TestDatabase",
CollectionNamePrefix = "Test."
});
});
}
PartialMongoDbConnectionOptions
Allows overriding individual MongoDB connection properties. Only non-null values are applied; all others fall back to the application's defaults.
| Property | Type | Default | Description |
|---|---|---|---|
DatabaseNamePrefix | string? | null | Prefix added to every database name. |
CollectionNamePrefix | string? | null | Prefix added to every collection name. |
DatabaseName | string? | null | Overrides the default database name. |
CaptureMongoDbCommandText | bool | true | Whether MongoDB command text is captured in telemetry/logging. |
HealthCheckMode | HealthCheckMode | Enabled | Controls how MongoDB health checks are reported. |
HealthCheckMode values
| Value | Description |
|---|---|
Enabled | Health checks run normally and report as healthy/unhealthy. |
EnabledButOverwriteUnhealthyWithDegraded | Unhealthy results are reported as degraded instead. |
SkipAsHealthy | Health checks are skipped and always reported as healthy. |