Introduction to ArchUnit
ArchUnit is an open-source library that offers a straightforward and flexible way to ensure the architectural integrity of Java applications. With ArchUnit, developers can automatically test and enforce coding and architectural rules within their projects.
Key Features
- Dependency Checking: ArchUnit allows users to examine the relationships between packages and classes, ensuring that dependencies align with the defined architecture.
- Layer and Slice Management: The library can check architectural layers and slices within the codebase, identifying potential cyclic dependencies and other architectural issues.
- Bytecode Analysis: ArchUnit analyzes Java bytecode to import all classes into a comprehensive code structure for thorough testing.
- Integration with Java Unit Testing Frameworks: The library seamlessly works with any standard Java unit testing framework, allowing architectural tests to be executed alongside other tests.
Getting Started
Adding ArchUnit to Your Project
To start using ArchUnit, you must include it as a dependency in your project:
-
Using Gradle:
testImplementation 'com.tngtech.archunit:archunit:1.3.0'
-
Using Maven:
<dependency> <groupId>com.tngtech.archunit</groupId> <artifactId>archunit</artifactId> <version>1.3.0</version> <scope>test</scope> </dependency>
Writing a Test
Here is a simple example to get you started with writing an architecture test:
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.ArchRule;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
public class MyArchitectureTest {
@Test
public void some_architecture_rule() {
JavaClasses importedClasses = new ClassFileImporter().importPackages("com.myapp");
ArchRule rule = classes()... // substitute with actual rule
rule.check(importedClasses);
}
}
Fluent API
The ArchUnit API is designed to be intuitive, guiding you through setting up and applying rules to check the code architecture effectively. It ensures a smooth experience as developers define complex architectural rules.
Further Resources
To explore more about ArchUnit, including in-depth usage examples and guidelines, visit the ArchUnit user guide or browse the ArchUnit Examples repository for practical examples.
Licensing
ArchUnit is distributed under the Apache License 2.0, with further details available at the provided license documentation. Additionally, ArchUnit utilizes several third-party libraries such as ASM under the BSD License and Google Guava under the Apache License 2.0. You can find all necessary licenses in the library’s licenses folder.
ArchUnit stands as a valuable tool for developers aiming to uphold the architectural standards of their Java applications, providing them with a powerful means to define and test the structure of their codebase.