How to do Test Driven Development in Clean Architecture?

Muthu
how-to-program
Published in
2 min readAug 20, 2023

--

“Clean Code is Everywhere and Everywhere is the Clean Code”

The Test Driven Development is the proven model to build the quality software.

Yes, it is mandatory to the developer to build the appliation bug free at the same time it must be clean. Otherwise, it will create more issues while the number of feature increasing.

As per the Test Driven Development (TDD), the development should start from writing test cases

The TDD’s approach starts with

  1. Writing test cases
  2. Fix the issues in development

While coming to the Clean Architecture, it is the process of software development which gives the guidelines to the developer/product to build a quality software with more flexibility.

If we want to merge these two concepts to the product development, the following questions may raise

  1. What are the components to be covered in testing?
  2. How and what to mock?
  3. etc…

Clean architecture components

If we want to implement TDD on CA, the components of CA needs to be very clear at high level

The popular diagram shows the different components and it’s placement with the purpose.

Personally, I am following this architecture with little changes

  1. Entities
  2. Providers (which interact with external systems. Equalent to Externam Interfaces )
  3. Repositories (The database related actions)
  4. Controllers (The API’s)
  5. Use cases

In this model, I am writing unit test cases for all the components except Providers.

I always create mock for the providers. The role of repositories to call the provider and get the details convert to Entities and send back to usecases. The usecases then send back the entities to the controllers.

--

--

Muthu
how-to-program

» 9+ years of experience in Data engineering, Dashboard designing » 3+ years of experience in Web application development