ArchUnit : Unit test your Java architecture

This blog gives an overview on how you can define Architectural principles / guidelines for your Java project using the ArchUnit f/w. The way we can Unit test the business capabilities we build, in a similar fashion if we want to test out whether our application is abiding / adhering to the architecture principles we can use ArchUnit.

ArchUnit enables us to codify the Architectural rules / guidelines that we want to implement in our project offering us the following benefits:

  • Catch architectural violations early in the development cycle
  • Ensure consistent architectural patterns / uniformity across different services within a project
  • Ease of integration

Following are some of the common use-cases where rules that can be integrated across different Java services / projects to assert that they are adhering to the architectural principles / guidelines that we can to enforce in our project.

  1. Enforcing a pre defined layered architecture
  2. Ensuring proper naming conventions for classes
  3. Ensuring best practices like Constructor based Dependency Injection in Spring based projects
  4. Enforcing the usage of certain libraries / APIs across different services in a project
  5. Avoidance of cyclic dependencies
  6. Enforcing Single Responsibility principle
  7. Enforcing rules for Constant classes
  8. Enforcing rules for Helper classes
  9. etc.

Add the following dependency to integrate ArchUnit in your project

<!-- https://mvnrepository.com/artifact/com.tngtech.archunit/archunit-junit5 -->
<dependency>
    <groupId>com.tngtech.archunit</groupId>
    <artifactId>archunit-junit5</artifactId>
    <version>1.4.1</version>
    <scope>test</scope>
</dependency>

The sample code for few Arch Rules implemented for a Spring Boot project is available @ https://github.com/kapilmonadi/ArchUnitSamples


So that’s it for this post. Go ahead and try this out in your favorite java editor.
See you until next time. Happy Learning !