Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

1

Share

Download to read offline

Unit test your architecture with ArchUnit

Download to read offline

The architecture of a system is usually a design that was made at a certain moment in time, and that fit the bill for the requirements at that time. However, as the system evolves and developers come and go, it’s hard to keep everyone up to date on a reference architecture or encourage developers to make changes to it, since it’s usually in the form of a set of documents, confluence pages, or something similar. This can result in code that violates architectural boundaries, piling up as technical debt.


ArchUnit, an architecture testing library for Java, enables you to formalize your architectural constraints as a set of ordinary unit tests. Using a fluent API, it enables you to formalize your architectural constraints in a descriptive way and run these constraints as a part of your build pipeline. Because ArchUnit tests are normal unit tests, they can evolve as your architecture changes, using version control to keep track of all changes.


In this presentation, lasting approximately 25 minutes, we’ll explore the ArchUnit library, and show you how you can use it to enforce your architectural constraints. A live demo is included.


We’ll cover the following topics:

• Getting started with ArchUnit
• Different checks (package dependencies, layers, inheritance, annotations) supported by ArchUnit
• Junit 4 and 5 support
• Rules as a separate module

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Unit test your architecture with ArchUnit

  1. 1. @MichelSchudel
  2. 2. Introduction www.craftsmen.nl michel@craftsmen.nl @MichelSchudel
  3. 3. https://martinfowler.com/ieeeSoftware/whoNeedsArchitect.pdf
  4. 4. Software architectural constraints as unit tests • Normal unit tests => versioning • Fluent API • Support for JUnit 4 and JUnit 5 • Extensible with custom rules • Open source Architectural rules across boundaries out of scope https://github.com/TNG/ArchUnit
  5. 5. <dependency> <groupId>com.tngtech.archunit</groupId> <artifactId>archunit-junit5</artifactId> <version>0.13.1</version> <scope>test</scope> </dependency> dependencies { testCompile 'com.tngtech.archunit:archunit-junit5:0.13.1' } Getting started
  6. 6. Check types • Package dependency checks • Class dependency checks • Naming and containment checks • Inheritance checks • Annotation checks • Layer checks • Cycle checks
  7. 7. api core client @AnalyzeClasses(packages = "nl.craftsmen.archunitdemo") public class ArchUnitJunit5Test { @ArchTest public static final ArchRule packageRule = noClasses() .that() .resideInAPackage("..core..") .should() .dependOnClassesThat() .resideInAnyPackage("..api..", "..client..."); } Package dependency checks No test methods wildcard
  8. 8. Let the API guide you
  9. 9. Class dependency check example classes() .that() .haveNameMatching(".*Bar") .should() .onlyBeAccessed() .byClassesThat() .haveSimpleName("Bar")
  10. 10. classes() .that() .areAssignableTo(EntityManager.class) .should() .onlyBeAccessed() .byClassesThat() .areAnnotatedWith(Transactional.class) Annotation checks
  11. 11. api core client @ArchTest public static final ArchRule layerRule = Architectures.layeredArchitecture() .layer("Api").definedBy("..api..") .layer("Core").definedBy("..core..") .layer("Client").definedBy("..client..") .whereLayer("Api") .mayNotBeAccessedByAnyLayer() .whereLayer("Core"). .mayOnlyBeAccessedByLayers("Api", "Client") .whereLayer("Client") .mayNotBeAccessedByAnyLayer(); Layer checks
  12. 12. Onion architecture public static final ArchRule onionRule = onionArchitecture() .domainModels("..core..") .domainServices("..core..") .applicationServices("..core..") .adapter("client", "..client..") .adapter("api", "..api..");
  13. 13. public static ArchCondition<JavaClass> notHaveFieldsAnnotatedWithDeprecated = new ArchCondition<JavaClass>("should not have field annotated with @Deprecated") { @Override public void check(JavaClass item, ConditionEvents events) { //if class has a deprecated field events.add(SimpleConditionEvent.violated(item, "@Deprecated not allowed")); } }; @ArchTest public static final ArchRule customRule = noClasses().should(notHaveFieldsAnnotatedWithDeprecated); Custom rules
  14. 14. nl:myarchtests:1.0.0 src/main/java/OurArchRules.java com.tech.archunit:archunit-junit5 scope: compile nl:myapp:1.0.0-SNAPSHOT src/main/java/..production code scope: test <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <dependenciesToScan> <dependency>nl:myarchtests:1.0.0</dependency> </dependenciesToScan> </configuration> </plugin> Checks as separate artifact @ArchTest public static final ArchRules shared = ArchRules.in(OurArchRules.class);
  15. 15. Demo time!
  16. 16. Summary • Nice addition to your project to enforce software architecture constraints • Unit test format, so easily runnable, versionable • Make a separate artifact to share between microservices
  17. 17. https://gitlab.com/craftsmen/archunit-demo @MichelSchudel
  • fccdinis

    Jun. 5, 2021

The architecture of a system is usually a design that was made at a certain moment in time, and that fit the bill for the requirements at that time. However, as the system evolves and developers come and go, it’s hard to keep everyone up to date on a reference architecture or encourage developers to make changes to it, since it’s usually in the form of a set of documents, confluence pages, or something similar. This can result in code that violates architectural boundaries, piling up as technical debt. ArchUnit, an architecture testing library for Java, enables you to formalize your architectural constraints as a set of ordinary unit tests. Using a fluent API, it enables you to formalize your architectural constraints in a descriptive way and run these constraints as a part of your build pipeline. Because ArchUnit tests are normal unit tests, they can evolve as your architecture changes, using version control to keep track of all changes. In this presentation, lasting approximately 25 minutes, we’ll explore the ArchUnit library, and show you how you can use it to enforce your architectural constraints. A live demo is included. We’ll cover the following topics: • Getting started with ArchUnit • Different checks (package dependencies, layers, inheritance, annotations) supported by ArchUnit • Junit 4 and 5 support • Rules as a separate module

Views

Total views

377

On Slideshare

0

From embeds

0

Number of embeds

18

Actions

Downloads

2

Shares

0

Comments

0

Likes

1

×