Skip to content

Commit

Permalink
introduce archUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan committed Jan 15, 2024
1 parent 77e1a88 commit 98c48bc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.binpastes.paste.api;

import com.github.binpastes.paste.domain.Paste;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Repository;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noMethods;

class ArchitectureTest {

private static final ImportOption DO_NOT_INCLUDE_MAVEN_TESTS = location -> !location.contains("/test-classes/");

@Test
void domainClassNeverRendered() {
var importedClasses = new ClassFileImporter()
.withImportOption(DO_NOT_INCLUDE_MAVEN_TESTS)
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS)
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.github.binpastes.paste.api");

var rule = noMethods()
.that()
.areDeclaredInClassesThat()
.resideInAPackage("com.github.binpastes.paste.api")
.should()
.haveRawReturnType(Paste.class);

rule.check(importedClasses);
}

@Test
void injectionsMindArchitecturalBorders() {
var importedClasses = new ClassFileImporter()
.withImportOption(DO_NOT_INCLUDE_MAVEN_TESTS)
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS)
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.github.binpastes.paste.api");

var rule = noClasses()
.that()
.resideInAPackage("com.github.binpastes.paste.api")
.should()
.dependOnClassesThat().areAnnotatedWith(Repository.class);

rule.check(importedClasses);
}
}

0 comments on commit 98c48bc

Please sign in to comment.