Alireza Farokhi
4 min readApr 15, 2022

--

Unit Testing Message Delivery in Message-Based Microservices — Part1(Setup Microservices)

Each microservice is an independent application deserving of all the
attention as any other application. A microservice has a focus on domain functionality. And they are pieces of a larger architectural solution. As such, these microservices have testing requirements just like any other application. However, they have more testing requirements because they are interconnected to form a bigger picture of that architectural solution

Why Messaging?

With messaging, there is a shift in the way of thinking about microservices. You can use simple messaging architecture to reach a microservice in a disconnected way.But moreover, using messaging with microservices allows for a dynamic and reactive architecture.Using messaging in the microservices architecture allows independent pieces to communicate without knowing the location of each other.

After we append messaging to our microservices we need to test if our microservices working properly about sending and receiving messages and message consumers. this can be test as Unit and Integration. I split this topic in to parts. in this part we are going to create microservices and setup messaging communication.

To demonstrate microservice messaging, we will create three projects: One for the Invoice Microservice which act as producer. Another for a Payment Microservice which act as consumer.Then a test client which act as a front-end service.

microservices use RabbitMQ as message broker .In our examples, we will run RabbitMQ from a Docker container. Docker Desktop is required to be installed. To install Docker Desktop, go to

https://docker.com/products/docker-desktop

With Docker Desktop installed, go to a command prompt and enter

docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management

Docker Desktop RabbitMQ

Now it’s time to create microservices.

Notice:for sake of simplicity and focus on messaging, our solution is going to splits into folders . In real world ,each microservice must be designed in well-form and appropriate architecture

I create a blank solution and add 3 folders

first we will create a class library on which the other projects will depend.
This project will contain the interfaces and classes that become our messages. Right-click “Contract” folder and create “Class Library” project and name it “MessageContracts” then add bellow classes :

Producer Microservice

Now we will create Invoice Microservice .This microservice is for processing invoices. It will receive a command to create an invoice. It will then publish an event about the new invoice created.

Right-click “Producer” folder and create “Console Application” project and name it “ Invoice Microservice” then add reference to “MessageContracts” and install “MassTransit.RabbitMQ” nuget package:

Install-Package MassTransit.RabbitMQ

add following code to program.cs

now we need to create consumer for this microserivce

this consumer ,consume events of type “InvoiceToCreate” then publish “InvoiceCreated” event for each invoice items.

Consumer Microservice

Now we will create the PaymentMicroservice project. This project will serve as an example of a downstream microservice that reacts to creating an invoice. Right-click “Consumer” folder and create “Console Application” project and name it “ PaymentMicroservice” then add reference to “MessageContracts” and install “MassTransit.RabbitMQ” nuget package:

Install-Package MassTransit.RabbitMQ

add following code to program.cs

now we need to create consumer for this microserivce

Client(front-end service)

We need to create a project that will play the role of front-end service.for simplicity, we will just have the code send a request for a new invoice
when the application starts.

Right-click the solution and create “Console Application” project and name it “ client” then add reference to “MessageContracts” and install “MassTransit.RabbitMQ” nuget package:

Install-Package MassTransit.RabbitMQ

add following code to program.cs

now we need to create consumer for this microserivce

So now every thing is down and out solution look like this:

now right-click solution select properties in then select multiple startup and select projects to start

run projects then in client press any key the result is as follow

In part2 we will write Unit Tests for message system

you can access to part2 by bellow link

download source code

Resource and more detail

--

--