How to Test Java Architecture with ArchUnit

sample code available on GitHub

Ruby Valappil


Photo by Samantha Hentosh on Unsplash


As a developer, we are not new to unit testing, we are also not new to writing efficient test cases using Pitest but how do we make sure that our architecture is also tested regularly?

Well, first of all, why do we need to test the architecture? Isn’t it defined once and followed ever after?

In most of the cases yes.

Monoliths remain monoliths until they become microservices and so on but what do we do to make sure packages are accessed as intended? Classes are named as intended? Interface and not a Class is referenced from another Class?

As the team size grows and the code base grows at a fast pace too, it's hard to keep track of all the commits and guidelines and as a result, bad code makes its way to the main branch, this is where ArchUnit comes to the rescue.

ArchUnit works well with junit so it could be a part of your test suite without adding any additional checks or tasks.

How to Add ArchUnit to a SpringBoot Project