top of page

Validating Springboot Application Architecture violation with ArchUnit

  • Writer: Anand Nerurkar
    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

==

  1. should be @RestController

  2. should return responseentity

  3. 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

  1. service class should not be @Componenet

  2. service should be annotated with @Service

  3. service class suffix should be service

  4. service class field should not be public

  5. bean method not allowed


Repository

===

Repository Rules Test

==

  1. class should be @Repository

  2. class sufix should be Repository

  3. 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.


 
 
 

Recent Posts

See All
Best Chunking Practices

1. Chunk by Semantic Boundaries (NOT fixed size only) Split by sections, headings, paragraphs , or logical units. Avoid cutting a sentence or concept in half. Works best with docs, tech specs, policie

 
 
 
Future State Architecture

USE CASE: LARGE RETAIL BANK – DIGITAL CHANNEL MODERNIZATION 🔹 Business Context A large retail bank wants to “modernize” its digital channels (internet banking + mobile apps). Constraints: Heavy regul

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2024 by AeeroTech. Proudly created with Wix.com

bottom of page