
As part of this article let’s try to understand what is Architecture Fitness Test and why do we even need it.
Architecture Fitness Test is a test which validates architecture design over a period of time. It can be manual or an automated process.
Why do we even have to test our architecture?
Testing the architecture will help many developers to know the entire architecture story, which was written by some of the experienced developers and came up with nice architecture diagrams.These architecture diagrams shows the system components and their interactions. But when project gets bigger, use cases becomes more complex, old team members dropped out and new team members joined, there were more cases where new feature got added to the project just to make it work. And gradually, every component will start depending on another, any change to one component has an undesired effect on other components. Even, some files resides in a undesired location, appearance of strange constructs of component, trying to force use cases into a component structure that is not at all fitting.
Specially, in an Agile project, where the role of the architect might even be distributed, there is more possibility of facing these issues frequently.
Solution
For the above scenarios there are two solutions,
- Some experienced developers can look into the code once in a week or so, identifies the violations and fix them
- Introduce automated validation process
With solution 1, there are couple of disadvantages, like, if there are less number of experienced developers, they’ll become the bottleneck to fix the violations. Also, this is a very time consuming process and they might feel overwhelmed with this new responsibility.
Advantages of Automated validation
With the automated validation process, couple of advantages comes with it,
- There is no bottleneck dependencies on experienced developers.
- As it’s a automated process, we can integrate with CI/CD
- Less time consuming
- When architecture rules evolves, it’s much easier to find out which old component needs migration
- Maintain healthy architecture quality
- Common understanding between developers
- New developers will have a much easier time to get acquainted with the code and get up to speed with the development process
We saw there are lots of advantages with automated process, but the question is how can we automate this process? 🤔 Here comes ArchUnit to save us 😎.
ArchUnit
ArchUnit is a free, simple and extensible library for checking the rchitecture of your Java code using any plain Java unit test framework. It can check dependencies between packages and classes, layers and slices, check for cyclic dependencies and more.
Advantages
ArchUnit comes with the following advantages,
- Easy to learn
- Write tests in simple Java
- Easily extensible
- Has simple english like syntax
- Write complex architecture checks with ease
- Out of the box support for Junit4 and Junit5
Even though it has the above advantages, unfortunately, it supports only Java language.
Now, let’s check what are the things we can validate out of the box with ArchUnit.
- Package Dependency Checks

2. Class Dependency Checks

3. Class and Package Containment Checks

4. Inheritance Checks

5. Annotation Checks

6. Layer Checks

7. Cycle Checks

In above examples it’s clear that we can validate complex architecture rules with ease using ArchUnit.
Example
Well, in the above section we got to know about the ArchUnit framework. In this section let’s see some real life example.
In my current project we are following the Hexagonal architecture pattern. Due to this pattern our package designs looks like the following

Hexagonal Architecture pattern is bit tricky for new joiners. We faced couple of above mentioned challenges using this architecture. To overcome those challenges, we have added the following test cases
These test cases are part of our build process. So, if anyone makes any code changes, they’ll get the quick feedback about the architecture design as well.
Conclusion
I hope you are able to understand the concept of Architecture Fitness Test and advantages of having automated validation process for the same. Even though, I focused on ArchUnit framework which works only on Java, but I believe there are different libraries available in the market for different programming language, so you can use this concept in almost every language.
PS: I hope you’ve enjoyed it, and if you’ve found it useful, please feel free to share with others or leave a comment. 😄
Originally posted on medium.com.