Member-only story
ArchUnit: Test Software Architecture Easily
How to validate architectural constraints in Java projects using ArchUnit
Why Should We Test the Architecture
As projects grow, the architecture becomes more complex. Each project has standard rules that developers need to follow. If new colleagues join the project, they might violate the architectural constraints without even knowing about it. If everyone adds new code wherever they think it fits, the code base will become messy.
It leads to laborious code maintenance and opens doors to bugs. Of course, there might be a senior developer who had been working on the project for years. They review the new code periodically and let the others know if they should fix anything.
The problem is that this requires manual intervention, and sometimes humans don’t spot all the issues. The best way of protecting the software architecture from violations is to have an automated process.
In this article, I’ll showcase the ArchUnit framework that tackles such issues. You’ll see typical practical examples to understand how to integrate this tool into your projects.
Let’s dive into it!
What Is ArchUnit
ArchUnit serves for unit testing Java projects architecture. It validates the architectural constraints. Usually, an architect or a lead developer establishes the general patterns. They write the rules in human-readable unit tests that can be evaluated with any unit testing tool like JUnit.
When someone breaks the rules, the tests will fail. Developers will see information about the issue. It ensures that the codebase stays intact and everyone follows the guidelines.
This art of testing is especially beneficial for new joiners to understand the project structure.
ArchUnit Demo
I assume you already have a Java project for testing, for example, a SpringBoot project.
First, add the ArchUnit maven dependency to your pom.xml
:
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>0.23.1</version>…