Member-only story

Spring Boot 3 — Unit Testing project Architecture with ArchUnit

Eric Anicet
5 min readDec 2, 2024

--

In this story, we’ll explore the power of ArchUnit in the Spring Boot application.

· Prerequisites
· Overview
What is ArchUnit?
Why use ArchUnit?
· Let’s get to the code
Project Architecture
Add ArchUnit Dependency
ArchUnit Rules Examples
Run the Tests
· Conclusion
· References

Prerequisites

This is the list of all the prerequisites:

  • Spring Boot 3+
  • Maven 3.6.3
  • Java 21
  • Basic knowledge with Junit 5

Overview

When building software, it’s common for development teams to define a set of guidelines and code conventions considered best practices.

These practices are generally documented and communicated to the entire development team that has accepted them. However, during development, developers can violate these guidelines discovered during code reviews or with code quality checking tools.

Therefore, to optimize the reviews, it is important to automate these directives as much as possible across the entire project architecture.

We can impose these guidelines as verifiable JUnit tests using ArchUnit. It guarantees that a software version will be discontinued if an architectural violation is introduced.

What is ArchUnit?

ArchUnit is a Java library designed to check the architecture of your code. It can check for cyclic dependencies, layers and slices, and dependencies between packages and classes, among other things. It analyzes given Java bytecode and imports all classes into a Java code structure. ArchUnit’s main focus is automatically testing architecture and coding rules, using any plain Java unit testing framework.

ArchUnit lets you implement rules for the static properties of application architecture in the form of executable tests such as the following:

--

--

Responses (1)

Write a response