Skip to main content

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)
ParameterDescription
builderThe IContainerBuilder for the testcontainer acting as a test tool (e.g. a service container used as a test dependency, not the SUT).
mongoDbContainerThe 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.
sectionNameThe configuration section name used by the application to read MongoDB options (e.g. "MasterConnectionOptions").
optionsOptional 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)
ParameterDescription
configurationBuilderThe configuration builder to add MongoDB settings to.
mongoDbContainerA started MongoDbContainer instance.
sectionNameThe configuration section name (e.g. "MasterConnectionOptions").
optionsOptional 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.

PropertyTypeDefaultDescription
DatabaseNamePrefixstring?nullPrefix added to every database name.
CollectionNamePrefixstring?nullPrefix added to every collection name.
DatabaseNamestring?nullOverrides the default database name.
CaptureMongoDbCommandTextbooltrueWhether MongoDB command text is captured in telemetry/logging.
HealthCheckModeHealthCheckModeEnabledControls how MongoDB health checks are reported.

HealthCheckMode values

ValueDescription
EnabledHealth checks run normally and report as healthy/unhealthy.
EnabledButOverwriteUnhealthyWithDegradedUnhealthy results are reported as degraded instead.
SkipAsHealthyHealth checks are skipped and always reported as healthy.