ConfluentKafkaAspNetAdapterTestTools 0.1.0
See the version list below for details.
dotnet add package ConfluentKafkaAspNetAdapterTestTools --version 0.1.0
NuGet\Install-Package ConfluentKafkaAspNetAdapterTestTools -Version 0.1.0
<PackageReference Include="ConfluentKafkaAspNetAdapterTestTools" Version="0.1.0" />
paket add ConfluentKafkaAspNetAdapterTestTools --version 0.1.0
#r "nuget: ConfluentKafkaAspNetAdapterTestTools, 0.1.0"
// Install ConfluentKafkaAspNetAdapterTestTools as a Cake Addin
#addin nuget:?package=ConfluentKafkaAspNetAdapterTestTools&version=0.1.0
// Install ConfluentKafkaAspNetAdapterTestTools as a Cake Tool
#tool nuget:?package=ConfluentKafkaAspNetAdapterTestTools&version=0.1.0
Introduction
This library is used to more easily test applications that has used the ConfluentKafkaAspNetAdapter.
Exempels
To use this library, add the nuget package to the project that will use it.
Application Factory Tests
This part of the library enables the mocking of the kafka buss by directly interacting with the underlying Kafka Consumer. This enables the publishing of mock messages that the registered IKafkaReceiver will pick up. It also allows for awaiting the completion of all processing that the receiver has to do.
Setup
To setup a Application Factory Test, follow the instructions from microsoft here and create an inherited ApplicationFactory or use the WithWebHostBuilder to configure the default implementation.
To get a hold of the Mock publisher call CreateMockMessagePublisher
within ConfigureWebHost
as follows:
builder.ConfigureTestServices(services =>
{
MessagePublisher = services.CreateMockMessagePublisher<NewDetaljplaneprojekt>();
});
Expose the MessagePublisher
to the test classes as appropriate.
Full example:
public class CustomWebApplicationFactory : WebApplicationFactory<Startup>
{
public IMockMessagePublisher<NewDetaljplaneprojekt> MessagePublisher { get; private set; }
public ProjectDatabaseContext DbContext => servieProvider.GetService<ProjectDatabaseContext>();
private ServiceProvider servieProvider;
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var sp = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddDbContext<ProjectDatabaseContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(sp);
});
var provider = services.BuildServiceProvider();
using(var scope = provider.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ProjectDatabaseContext>();
db.Database.EnsureCreated();
}
servieProvider = provider;
});
builder.ConfigureTestServices(services =>
{
MessagePublisher = services.CreateMockMessagePublisher<NewDetaljplaneprojekt>();
});
}
}
Note that this also sets up a InMemory Db
Testing
To use the MessagePublisher
begin with specifying the expected number of calls that it will receive:
MessagePublisher.ExpectedNumberOfCalls(2);
This will enable you to await the asynchronous execution of the configured KafkaReceiver
.
To publish mocked messages, simply call:
MessagePublisher.Publish(publishMessage1);
MessagePublisher.Publish(publishMessage2);
Then to await the processing, call:
await MessagePublisher.AwaitFinished();
Full Example:
[Test]
public async Task Publish_Same_Project_Twice_Should_Only_Save_First()
{
// Arange
await SeedTestUser();
// Act
factory.MessagePublisher.ExpectedNumberOfCalls(2);
factory.MessagePublisher.Publish(publishProject);
factory.MessagePublisher.Publish(publishProject);
await factory.MessagePublisher.AwaitFinished();
// Assert
Assert.That(factory.DbContext.Detaljplaneprojekts.Count(), Is.EqualTo(1));
}
Note that here I've exposed the MessagePublisher
via the WebApplicationFactory
.
Live Kafka Tests
This part of the library isn't as well tested, but it's used to interact with a running Kafka bus. The functionality is exposed
through the KafkaAdapterTestContext
.
Setup
The KafkaAdapterTestContext
takes the uri's for the running Kafka bus and SchemaRegistry as it's constructor arguments:
[SetUp]
public void SetUp()
{
kafkaAdapterTestContext = new KafkaAdapterTestContext<TestMessage>("localhost:9092", "localhost:8081");
}
The type parameter is the message type that should be handled
Verify Message published
To verify that a message has been published, a TestReceiver must be started:
kafkaAdapterTestContext.StartTestReceiver("Golden");
The parameter is the topic it's supposed to listen to. To verify that the message is published:
Assert.That(kafkaAdapterTestContext.VerifyMessagePublished());
This will poll the testreceiver each second for 15 seconds to see whether the message has been received.
Verify Message received
To verify that a message is handled, we expose the capability to publish messages to the Kafka bus via the context:
kafkaAdapterTestContext.PublishMessage("Golden", new TestMessage { TestField = "Test" });
And then the effects of the processing of the messge can be verified.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.2 is compatible. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.2
- CompareNETObjects (>= 4.59.0)
- ConfluentKafkaAspNetAdapter (>= 0.2.2)
- NSubstitute (>= 4.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ConfluentKafkaAspNetAdapterTestTools:
Package | Downloads |
---|---|
MachifuAuthentication.TestUtils
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.