DEV Community

Alexsandro Souza
Alexsandro Souza

Posted on

Are you writing automated tests for your architecture?

I am always posting provoking post on my Linkedin, and I have decided to replicate those here. Let's socialize!

Once upon a time, somebody experienced looked at the code and drew up some nice architecture diagrams, showing the components the system should consist of and how they should interact. However, when the project got bigger, there were more and more cases where new features would just be added in any way that fits.

That is it! A big ball of mud was just created.

The good news is that there are several free tools out there to automatically test for dependencies between packages and classes and ensure the architecture decisions are followed.

Simply put, there are test libraries that allow us to verify that an application adheres to a given set of architectural rules.

Here is an example using ArchUnit(Java library).

JavaClasses jc = new ClassFileImporter().importPackages("com.apssouza.tradingengine");

LayeredArchitecture arch = layeredArchitecture() 
// Define layers 
.layer("Presentation").definedBy("..presentation..")
.layer("Service").definedBy("..service..") 
.layer("Persistence").definedBy("..persistence..") 
// Add constraints 
.whereLayer("Presentation").mayNotBeAccessedByAnyLayer() 
.whereLayer("Service").mayOnlyBeAccessedByLayers("Presentation") 
.whereLayer("Persistence").mayOnlyBeAccessedByLayers("Service"); 

arch.check(jc);
Enter fullscreen mode Exit fullscreen mode

The link between architecture traits and maintainability is a well-studied topic in the software industry. Defining a suitable architecture for our systems is not enough, though. We need to make sure that the code implemented adheres to it.

Please check out my post explaining how to structure code effectively

Top comments (0)