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


ree

General Coding rules

===

ree
ree

Controller

==

ree

Controller Rules Test

==

  1. should be @RestController

  2. should return responseentity

  3. should be with valid mapping


ree
ree

Model Rules Test

===

ree

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

ree

if model is not annotated @Entity, below test fails

ree

if filed is public, then test fails as below

ree

Service

====

ree

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

ree
ree

Repository

===

ree

Repository Rules Test

==

  1. class should be @Repository

  2. class sufix should be Repository

  3. it should be interface


ree

if not interface, test fail below


ree

last test is layered architecture test

==

This is test where we define layerd architecture as below.

ree

 
 
 

Recent Posts

See All

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