Arc unit test

 

1. What is ArchUnit?

Answer:
ArchUnit is an open-source Java testing library used to validate and enforce application architecture rules (e.g., layered architecture, naming conventions, dependency rules) through unit-like tests.


3. What architectural violations are most important to catch with ArchUnit in enterprise systems?

Ideal Answer

  • Cyclic dependencies between modules

  • Service-to-service direct DB calls

  • Cross-domain leakage (bounded context breaches)

  • Avoiding util-god packages

  • Only domain layer uses domain entities

  • Enforce hexagonal architecture (ports/adapters)

  • REST controllers don’t call repositories directly


4. What type of rules can we enforce with ArchUnit?

Answer:

  • Layered architecture rules

  • Package structure rules

  • Naming conventions

  • Class dependency restrictions

  • Annotation usage rules

  • Cyclic dependency detection


2. Why do we use ArchUnit?

Answer:

  • To enforce architectural standards

  • Prevent bad dependencies

  • Ensure layered structure (Controller → Service → Repository)

  • Detect cyclic dependencies early

  • Improve long-term maintainability


    9. Can we integrate ArchUnit with CI/CD?

    Answer:
    Yes — run ArchUnit tests as part of your normal test pipeline in Maven/Gradle CI tools like GitHub Actions, Jenkins, GitLab CI, etc.


    10. What happens when rules fail?

    Answer:
    The test fails with a descriptive message, preventing merge/deployment if enforced in CI.


    11. Difference between ArchUnit and SonarLint/SonarQube?

    ArchUnitSonarLint/SonarQube
    Developer-defined rulesBuilt-in + custom rules
    Architecture test at unit levelMostly static code quality
    Runtime JUnit testStatic code scan

    3. How do you add ArchUnit dependency?

    Answer:
    Maven

    <dependency> <groupId>com.tngtech.archunit</groupId> <artifactId>archunit-junit5</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency>


    1. How would you ensure architectural governance in a large microservices ecosystem using ArchUnit?

    Expected Talking Points

    • Create reusable architectural rule library shared across repos

    • Validate:

      • Layer boundaries (Controller → Service → Domain → Persistence)

      • Avoid direct DB access in controllers

      • Rule-based module visibility & API boundaries

      • Annotation enforcement (e.g., @Transactional@Service)

    • Integrate into CI so code violating architecture cannot merge

    5. Example: Enforce Controller naming convention

    Question: Write ArchUnit rule so that all controllers end with Controller.

    Answer:

    @AnalyzeClasses(packages = "com.app") public class NamingConventionTest { @ArchTest static final ArchRule controllersShouldBeNamedProperly = classes().that().areAnnotatedWith(RestController.class) .should().haveSimpleNameEndingWith("Controller"); }

    6. Enforce layered architecture

    Question: Write rule where

    • Controllers only call services

    • Services only call repositories

    Answer:

    @AnalyzeClasses(packages = "com.app") public class LayeredArchitectureTest { @ArchTest Static ArchRule layeredArchitecture = layeredArchitecture() .layer("Controller").definedBy("..controller..") .layer("Service").definedBy("..service..") .layer("Repository").definedBy("..repository..") .whereLayer("Controller").mayOnlyAccessLayers("Service") .whereLayer("Service").mayOnlyAccessLayers("Repository"); }

    7. How do you check for cyclic dependencies?

    Answer:

    @AnalyzeClasses(packages = "com.app") class CyclicDependencyTest { @ArchTest static final ArchRule noCycles = slices().matching("com.app.(*)..") .should().beFreeOfCycles(); }

    8. Can ArchUnit test annotation usage?

    Answer: Yes

    Example: All services must have @Service annotation

    static ArchRule servicesShouldBeAnnotated = classes().that().resideInAPackage("..service..") .should().beAnnotatedWith(Service.class);

    2. How do you scale ArchUnit tests without slowing down builds in large mono-repos?

    Expected Answer

    • Test only architecture-critical modules

    • Use selective package scans (avoid scanning all classes)

    • Use @AnalyzeClasses(importOptions = ImportOption.DoNotIncludeTests.class)

    • Cache class scans

    • Run full architecture checks nightly; light checks per PR

Comments

Popular posts from this blog

Archunit test

Hexagonal Architecture

visitor design pattern