avatar

Bhuwan Upadhyay

Talks all about software engineering

Published on

Enforce onion architecture in your code

Authors

Introduction

ArchUnit's main focus is to automatically test architecture and coding rules. In this code snippet, I will show how to use ArchUnit to enforce architecture in your code.

To create something exceptional, your mindset must be relentlessly focused on the smallest detail. - Giorgio Armani

<dependency>
    <groupId>com.tngtech.archunit</groupId>
    <artifactId>archunit-junit5</artifactId>
    <!-- <artifactId>archunit-junit4</artifactId> -->
    <version>0.14.1</version>
    <scope>test</scope>
</dependency>

Bounded Context-Package Artifacts

Image

ArchUnit Test

@AnalyzeClasses(packages = CodingRuleTest.PACKAGE)
class CodingRuleTest {

    public static final String PACKAGE = "io.retailstore.cart";

    @ArchTest
    private final ArchRule classes_are_under_packages = ArchRuleDefinition.classes()
            .should()
            .resideInAnyPackage(PACKAGE, "..application..", "..domain..", "..infrastructure..", "..interfaces..");
            
    @ArchTest
    private final ArchRule onion_dependencies_are_respected = Architectures
            .onionArchitecture()
            .domainModels("..domain.model..")
            .domainServices("..domain.services..")
            .applicationServices("..application.commandservices..", "..application.queryservices..")
            .adapter("outbound", "..infrastructure..", "..application.outboundservices..")
            .adapter("inbound", "..interfaces..");
}