Validating Springboot Application Architecture violation with ArchUnit
- Anand Nerurkar
- Aug 6, 2024
- 2 min read
Let us take example of API development.
As per the best proctices, we do follow layer based architecture as below
Controller-->will use Model -> invoke --->Service layer ---> will invoke repository layer
There will be rules set for each layer for example
contoller classes should be annotated as @RestController
model classes should be annotated as @Entity, @Table
Service classes should be classes ,not interfaces
it should be annotated as @service
As a part of development, each developer will be contributing , but how do we make sure that they are following the rules, so to validated that we make use of ArchUnit that will describe the rules for each layer and any violation will be triggered as errors.
Please include beloe in pom.xml
General Coding rules
===
Controller
==
Controller Rules Test
==
should be @RestController
should return responseentity
should be with valid mapping
Model Rules Test
===
Model Rules
==
1 . IT should be annotated as @Entity, @Table
2. model fiels should not be public
3. model field should be @Column
if test success, then it is following rule , else will flag error
if model is not annotated @Entity, below test fails
if filed is public, then test fails as below
Service
====
Service Rules that need to be followed as below. if any violation, will report it as part of test execution
service class should not be @Componenet
service should be annotated with @Service
service class suffix should be service
service class field should not be public
bean method not allowed
Repository
===
Repository Rules Test
==
class should be @Repository
class sufix should be Repository
it should be interface
if not interface, test fail below
last test is layered architecture test
==
This is test where we define layerd architecture as below.
Comments