Testing Standards
Structural Validation (ArchUnit)
Section titled “Structural Validation (ArchUnit)”To ensure our Hexagonal Architecture remains pure, we enforce architectural rules via tests. Every new service must utilize ArchUnit to validate dependencies.
Required Rules
Section titled “Required Rules”- Domain Isolation: The Domain layer must NOT depend on Application or Infrastructure.
- Violation: Importing a Controller into an Entity.
- Infrastructure Isolation: The Infrastructure layer can depend on everything, but nothing should depend on Infrastructure (except main/bootstrapping).
- Naming Conventions: Classes must follow the naming standards defined in Hexagonal Architecture.
Example (Java - ArchUnit)
Section titled “Example (Java - ArchUnit)”package com.company.project;
import com.tngtech.archunit.core.importer.ImportOption;import com.tngtech.archunit.junit.AnalyzeClasses;import com.tngtech.archunit.junit.ArchTest;import com.tngtech.archunit.lang.ArchRule;
import static com.tngtech.archunit.library.Architectures.onionArchitecture;
@AnalyzeClasses(packages = "com.company.project", importOptions = ImportOption.DoNotIncludeTests.class)public class ArchitectureTest {
@ArchTest static final ArchRule hexagonal_architecture_is_respected = onionArchitecture() .domainModels("..domain.model..") .domainServices("..domain.service..") .applicationServices("..application..") .adapter("persistence", "..infrastructure.persistence..") .adapter("web", "..infrastructure.web..");}Testing Pyramid
Section titled “Testing Pyramid”- Unit Tests: Cover 100% of Domain Logic. Fast, no Spring Context (
@SpringBootTest). Use JUnit 5 + Mockito. - Integration Tests: Verify Adapters (
@DataJpaTest,@WebMvcTest). - E2E Tests: Verify critical flows with full context (
@SpringBootTest).