add ArchUnitTest

This commit is contained in:
Michael Hoennig 2022-08-02 15:17:24 +02:00
parent 2ac476d99b
commit 142aac2bce
4 changed files with 68 additions and 3 deletions

View File

@ -44,6 +44,7 @@ dependencies {
testImplementation 'org.testcontainers:testcontainers'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:postgresql'
testImplementation 'com.tngtech.archunit:archunit-junit5:1.0.0-rc1'
}
dependencyManagement {

View File

@ -1,7 +1,6 @@
package net.hostsharing.hsadminng.hscustomer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.UUID;

View File

@ -0,0 +1,67 @@
package net.hostsharing.hsadminng.arch;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.RestController;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices;
@AnalyzeClasses(packages = ArchUnitTest.NET_HOSTSHARING_HSADMINNG)
public class ArchUnitTest {
public static final String NET_HOSTSHARING_HSADMINNG = "net.hostsharing.hsadminng";
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule contextPackageRule = classes()
.that().resideInAPackage("..context..")
.should().onlyDependOnClassesThat()
.resideOutsideOfPackage(NET_HOSTSHARING_HSADMINNG);
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule configPackageRule = classes()
.that().resideInAPackage("..config..")
.should().onlyDependOnClassesThat()
.resideOutsideOfPackage(NET_HOSTSHARING_HSADMINNG);
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule errorsPackageRule = classes()
.that().resideInAPackage("..errors..")
.should().onlyDependOnClassesThat()
.resideOutsideOfPackage(NET_HOSTSHARING_HSADMINNG);
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule hsPackagesRule = classes()
.that().resideInAPackage("..hs*")
.should().onlyBeAccessed().byClassesThat()
.resideInAnyPackage("..hs*");
@ArchTest
@SuppressWarnings("unused")
public static final ArchRule hsPackagePackageRule = classes()
.that().resideInAPackage("..hspackage..")
.should().onlyBeAccessed().byClassesThat()
.resideInAnyPackage("..hspackage..");
@Test
public void everythingShouldBeFreeOfCycles() {
slices().matching("net.hostsharing.hsadminng.(*)..").should().beFreeOfCycles();
}
@Test
public void restControllerNaming() {
classes().that().areAnnotatedWith(RestController.class).should().haveSimpleNameEndingWith("Controller");
}
@Test
public void repositoryNaming() {
classes().that().implement(JpaRepository.class).should().haveSimpleNameEndingWith("Repository");
}
}

View File

@ -10,8 +10,6 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.UUID.randomUUID;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.*;