Kafka.TestBroker 1.0.0

dotnet add package Kafka.TestBroker --version 1.0.0
                    
NuGet\Install-Package Kafka.TestBroker -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kafka.TestBroker" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kafka.TestBroker" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Kafka.TestBroker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kafka.TestBroker --version 1.0.0
                    
#r "nuget: Kafka.TestBroker, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kafka.TestBroker@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kafka.TestBroker&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Kafka.TestBroker&version=1.0.0
                    
Install as a Cake Tool

Kafka.TestBroker

An in-process fake Kafka broker for integration tests and local development. No Docker, no Zookeeper, no external infrastructure — just add the package and go.

Installation

dotnet add package Kafka.TestBroker

Quick start

using Kafka.TestFramework;

var settings = new BrokerSettings
{
    Host = "127.0.0.1",
    Port = 0,   // 0 = random free port
    Topics = new List<TopicSettings>
    {
        new TopicSettings { Name = "orders", Partitions = 1 }
    }
};

await using var broker = new TestBroker(settings);
await broker.StartAsync();

Console.WriteLine(broker.BootstrapServers); // e.g. "127.0.0.1:54321"

Pass broker.BootstrapServers to your Confluent.Kafka producer or consumer — they talk to the fake broker exactly as they would to a real one.

Usage in tests

[Test]
public async Task Order_Is_Processed(CancellationToken cancellationToken)
{
    var settings = new BrokerSettings
    {
        Host = "127.0.0.1",
        Port = 0,
        Topics = new List<TopicSettings>
        {
            new TopicSettings { Name = "orders", Partitions = 1 }
        }
    };

    await using var broker = new TestBroker(settings);
    await broker.StartAsync();

    // Produce
    using var producer = new ProducerBuilder<string, string>(new ProducerConfig
    {
        BootstrapServers = broker.BootstrapServers
    }).Build();

    await producer.ProduceAsync("orders", new Message<string, string>
    {
        Key = "order-1",
        Value = """{ "id": 1, "item": "widget" }"""
    }, cancellationToken);
    producer.Flush(cancellationToken);

    // Consume
    using var consumer = new ConsumerBuilder<string, string>(new ConsumerConfig
    {
        BootstrapServers = broker.BootstrapServers,
        GroupId           = "test-group",
        AutoOffsetReset   = AutoOffsetReset.Earliest,
        EnableAutoCommit  = false
    }).Build();

    consumer.Subscribe("orders");

    var result = consumer.Consume(TimeSpan.FromSeconds(5));

    Assert.Equal("order-1", result.Message.Key);
    Assert.Contains("widget", result.Message.Value);
}

BrokerSettings reference

Property Default Description
Host "localhost" IP or hostname the broker binds to
Port 0 TCP port. 0 picks a random free port
Topics [] Topics the broker advertises in Metadata responses
Groups [] Consumer groups (informational)

broker.BootstrapServers always returns the actual host:port string the socket is bound to.

Multiple topics and partitions

var settings = new BrokerSettings
{
    Host = "127.0.0.1",
    Port = 0,
    Topics = new List<TopicSettings>
    {
        new TopicSettings { Name = "events",  Partitions = 3 },
        new TopicSettings { Name = "metrics", Partitions = 1 }
    }
};

Error injection

Simulate broker failures to test retry / error handling paths:

// Make partition 0 of "orders" return LEADER_NOT_AVAILABLE (error 5)
broker.SetPartitionError("orders", partition: 0, errorCode: 5);

// ... run your code that should handle the error ...

// Restore normal operation
broker.ClearPartitionError("orders", partition: 0);

Common error codes:

Code Kafka name
3 UNKNOWN_TOPIC_OR_PARTITION
5 LEADER_NOT_AVAILABLE
6 NOT_LEADER_FOR_PARTITION
9 REPLICA_NOT_AVAILABLE
10 MESSAGE_TOO_LARGE

Use in a console app or hosted service

TestBroker has no ASP.NET Core dependency — it can run anywhere:

// Plain console app
var settings = new BrokerSettings
{
    Host = "127.0.0.1",
    Port = 19093,
    Topics = new List<TopicSettings>
    {
        new TopicSettings { Name = "my-topic", Partitions = 1 }
    }
};

await using var broker = new TestBroker(settings);
await broker.StartAsync();

Console.WriteLine($"Broker listening on {broker.BootstrapServers}");
Console.WriteLine("Press Ctrl+C to stop...");
await Task.Delay(Timeout.Infinite);

Or register it in a hosted service:

// Program.cs (ASP.NET Core / Generic Host)
builder.Services.AddSingleton<TestBroker>(sp =>
{
    var settings = sp.GetRequiredService<IConfiguration>()
        .GetSection("BrokerSettings")
        .Get<BrokerSettings>() ?? new BrokerSettings();
    return new TestBroker(settings);
});
builder.Services.AddHostedService<BrokerHostedService>();

What the fake broker supports

Kafka API Supported
ApiVersions
Metadata
Produce
Fetch ✅ (with MaxWaitMs long-poll)
OffsetFetch
OffsetCommit
ListOffsets
FindCoordinator
JoinGroup ✅ (multi-consumer rebalance)
SyncGroup
Heartbeat
LeaveGroup
InitProducerId ✅ (idempotent producers)
GetTelemetrySubscriptions

Tested with Confluent.Kafka 2.x.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 39 5/24/2026