.NET - Testing system with message broker

85 Views Asked by At

I have a question about testing my application where I use a message broker (mass transit library, Azure Service Bus implementation) to communicate between services. Let's say I have ServiceA and ServiceB.

As far as integration tests are concerned, I guess it's clear there I just start up ServiceB and Message broker and test. However, I was more thinking if there is such a thing as contract tests with message broker? I tried searching and googling and couldn't find anything much.

Thanks a lot in advance.

Edit: ServiceA - This method send over mass transit topic message

public async Task Send()
{
  await _publishEndpoint.Publish(new TestMessage(), cancellationToken);
}

ServiceB - Here is consumer of TestMessage

  public class TestConsumer : IConsumer<TestMessage>
  {
      public async Task Consume(ConsumeContext<TestMessage> context)
      {
          // Do some work
      }
  }
1

There are 1 best solutions below

0
Sampath On

The below code is for ServiceA and ServiceB along with an integration test to test the message flow between them using Azure Service Bus with MassTransit:


    {
        var busControl = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
        {
            cfg.Host("your-connection-string");
        });

        await busControl.StartAsync();

        var serviceA = new ServiceA(busControl);
        await serviceA.Send();

        await busControl.StopAsync();
    }
}

public class ServiceA
{
    private readonly IBusControl _busControl;

    public ServiceA(IBusControl busControl)
    {
        _busControl = busControl;
    }

    public async Task Send()
    {
        await _busControl.Publish(new TestMessage { Content = "Hello from ServiceA!" });
        Console.WriteLine("Message sent from ServiceA");
    }
}

public class TestMessage
{
    public string Content { get; set; }
}



Test cases:


[Test]
    public async Task TestMessageFlow()
    {
        // Arrange
        var busControl = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
        {
            cfg.Host("your-connection-string");
        });

        await busControl.StartAsync();

        var serviceA = new ServiceA(busControl);
        var serviceB = new ServiceB(busControl);

        // Act
        await serviceB.StartListening();
        await serviceA.Send();

        // Assert
        // You can add assertions here to verify that ServiceB has received and processed the message
        // For example, check logs, database state, etc.

        await busControl.StopAsync();
    }
}

public class ServiceA
{
    private readonly IBusControl _busControl;

    public ServiceA(IBusControl busControl)
    {
        _busControl = busControl;
    }

    public async Task Send()
    {
        await _busControl.Publish(new TestMessage { Content = "Hello from ServiceA!" });
    }
}



enter image description here

enter image description here

  • Another method is to use MassTransit's test harness for unit and integration testing.