generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Added ArchUnit for better Consistency in Test-Classes #1206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DanielOber
merged 25 commits into
main
from
758-feature-archunit---validate-test-naming-convention---backend
Mar 12, 2026
Merged
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6ac0961
added archunit and fixed tests
DanielOber 8bcf652
spotless
DanielOber 41d5b5c
code rabbit review
DanielOber 6ba6a2b
code rabbit review
DanielOber 429c843
Merge branch 'main' into 758-feature-archunit---validate-test-naming-…
DanielOber 2a308f4
Merge branch 'main' into 758-feature-archunit---validate-test-naming-…
DanielOber 564bca9
renaming
DanielOber 8988021
Merge remote-tracking branch 'origin/758-feature-archunit---validate-…
DanielOber 8cc94f3
coderabbit review eingearbeitet
DanielOber 8910444
Merge branch 'main' into 758-feature-archunit---validate-test-naming-…
DanielOber a5fb268
Merge branch 'main' into 758-feature-archunit---validate-test-naming-…
devtobi 4c5f042
moved rule stream to the top
DanielOber 0ae4fbd
Merge remote-tracking branch 'origin/758-feature-archunit---validate-…
DanielOber 673916e
better naming for archunit tests
DanielOber 16de0a0
Merge branch 'main' into 758-feature-archunit---validate-test-naming-…
DanielOber 10f211f
removed empty line
DanielOber f3f2106
changed path to class
DanielOber d98fc05
spottless
DanielOber 8b9f6c6
Update refarch-backend/src/test/java/de/muenchen/oss/refarch/backend/…
DanielOber bd977a2
Update refarch-backend/src/test/java/de/muenchen/oss/refarch/backend/…
DanielOber 8232389
review feedback eingearbeitet
DanielOber 686b24f
Merge remote-tracking branch 'origin/758-feature-archunit---validate-…
DanielOber 6da281d
spottless
DanielOber 4083b22
Update refarch-backend/src/test/java/de/muenchen/refarch/archunit/rul…
DanielOber f8ea154
Update refarch-backend/src/test/java/de/muenchen/refarch/archunit/rul…
DanielOber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
refarch-backend/src/test/java/de/muenchen/refarch/archunit/ArchUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package de.muenchen.refarch.archunit; | ||
|
|
||
| import com.tngtech.archunit.core.domain.JavaClasses; | ||
| import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
| import com.tngtech.archunit.core.importer.ImportOption; | ||
| import com.tngtech.archunit.lang.ArchRule; | ||
| import de.muenchen.refarch.archunit.rules.Rules; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.aggregator.ArgumentsAccessor; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| public class ArchUnitTest { | ||
|
|
||
| private static JavaClasses allTestClasses; | ||
|
|
||
| @BeforeAll | ||
| static void init() { | ||
| allTestClasses = new ClassFileImporter() | ||
| .withImportOption(new ImportOption.OnlyIncludeTests()) | ||
| .importPackages(de.muenchen.refarch.MicroServiceApplication.class.getPackage().getName()); | ||
| } | ||
devtobi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("allTestClassesRulesToVerify") | ||
| void givenAllArchUnitRulesForAllTestClasses_thenRunArchUnitTests(final ArgumentsAccessor arguments) { | ||
| arguments.get(1, ArchRule.class).check(allTestClasses); | ||
| } | ||
DanielOber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static Stream<Arguments> allTestClassesRulesToVerify() { | ||
DanielOber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Stream.of( | ||
| Arguments.of("RULE_TESTCLASSES_END_WITH_TEST_CONVENTION_MATCHED", | ||
| Rules.RULE_TESTCLASSES_END_WITH_TEST_CONVENTION_MATCHED), | ||
| Arguments.of("TEST_NAMING_CONVENTION_RULE", Rules.RULE_TEST_NAMING_CONVENTION_GIVEN_THEN_MATCHED), | ||
| Arguments.of("RULE_BEFORE_EACH_NAMING_CONVENTION_MATCHED", Rules.RULE_BEFORE_EACH_NAMING_CONVENTION_MATCHED), | ||
| Arguments.of("RULE_AFTER_EACH_NAMING_CONVENTION_MATCHED", Rules.RULE_AFTER_EACH_NAMING_CONVENTION_MATCHED), | ||
| Arguments.of("TEST_METHODS_ARE_PACKAGE_PRIVATE_CONVENTION_MATCHED", Rules.RULE_TEST_METHODS_ARE_PACKAGE_PRIVATE_CONVENTION_MATCHED)); | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
51 changes: 51 additions & 0 deletions
51
refarch-backend/src/test/java/de/muenchen/refarch/archunit/rules/Rules.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package de.muenchen.refarch.archunit.rules; | ||
|
|
||
| import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; | ||
| import static de.muenchen.refarch.archunit.rules.TestClassesEndWithTestCondition.haveTopEnclosingClassEndingWithTest; | ||
|
|
||
| import com.tngtech.archunit.core.domain.JavaModifier; | ||
| import com.tngtech.archunit.lang.ArchRule; | ||
| import lombok.AccessLevel; | ||
| import lombok.NoArgsConstructor; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.RepeatedTest; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestTemplate; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
DanielOber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public final class Rules { | ||
|
|
||
| public static final ArchRule RULE_TEST_NAMING_CONVENTION_GIVEN_THEN_MATCHED = methods() | ||
| .that().areAnnotatedWith(Test.class) | ||
| .or().areAnnotatedWith(ParameterizedTest.class) | ||
| .or().areAnnotatedWith(RepeatedTest.class) | ||
| .or() | ||
| .areAnnotatedWith(TestTemplate.class) | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .should().haveNameMatching("^given[A-Z][a-zA-Z]+_then[A-Z][a-zA-Z]+$"); | ||
DanielOber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static final ArchRule RULE_BEFORE_EACH_NAMING_CONVENTION_MATCHED = methods() | ||
| .that().areAnnotatedWith(BeforeEach.class).should().haveNameMatching("^setUp$") | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .allowEmptyShould(true); | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static final ArchRule RULE_AFTER_EACH_NAMING_CONVENTION_MATCHED = methods() | ||
| .that().areAnnotatedWith(AfterEach.class).should().haveNameMatching("^tearDown$") | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .allowEmptyShould(true); | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static final ArchRule RULE_TEST_METHODS_ARE_PACKAGE_PRIVATE_CONVENTION_MATCHED = methods() | ||
| .that().areAnnotatedWith(Test.class) | ||
| .or().areAnnotatedWith(ParameterizedTest.class) | ||
| .or().areAnnotatedWith(RepeatedTest.class).or() | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .areAnnotatedWith(TestTemplate.class).should() | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .notHaveModifier(JavaModifier.PROTECTED) | ||
| .andShould().notHaveModifier(JavaModifier.PRIVATE) | ||
| .andShould().notHaveModifier(JavaModifier.PUBLIC); | ||
|
|
||
| public static final ArchRule RULE_TESTCLASSES_END_WITH_TEST_CONVENTION_MATCHED = methods() | ||
| .that().areAnnotatedWith(Test.class) | ||
| .or().areAnnotatedWith(ParameterizedTest.class) | ||
| .or().areAnnotatedWith(RepeatedTest.class).or() | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .areAnnotatedWith(TestTemplate.class) | ||
DanielOber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .should(haveTopEnclosingClassEndingWithTest); | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
...end/src/test/java/de/muenchen/refarch/archunit/rules/TestClassesEndWithTestCondition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package de.muenchen.refarch.archunit.rules; | ||
|
|
||
| import com.tngtech.archunit.core.domain.JavaClass; | ||
| import com.tngtech.archunit.core.domain.JavaMethod; | ||
| import com.tngtech.archunit.lang.ArchCondition; | ||
| import com.tngtech.archunit.lang.ConditionEvents; | ||
| import com.tngtech.archunit.lang.SimpleConditionEvent; | ||
|
|
||
| public class TestClassesEndWithTestCondition extends ArchCondition<JavaMethod> { | ||
|
|
||
| TestClassesEndWithTestCondition() { | ||
| super("have top enclosing class name ending with `Test`"); | ||
| } | ||
|
|
||
| public static final ArchCondition<JavaMethod> haveTopEnclosingClassEndingWithTest = new TestClassesEndWithTestCondition(); | ||
|
|
||
| @Override | ||
| public void check(JavaMethod method, ConditionEvents events) { | ||
| final var topEnclosingClass = getTopEnclosingClass(method.getOwner()); | ||
|
|
||
| if (!topEnclosingClass.getSimpleName().endsWith("Test")) { | ||
| events.add(SimpleConditionEvent.violated(method, "Method %s must be declared in a class whose simple name ends with 'Test' (found: %s)" | ||
| .formatted(method.getName(), topEnclosingClass.getSimpleName()))); | ||
| } | ||
| } | ||
|
|
||
| private JavaClass getTopEnclosingClass(JavaClass item) { | ||
| while (item.getEnclosingClass().isPresent()) { | ||
| item = item.getEnclosingClass().orElseThrow(); | ||
| } | ||
| return item; | ||
| } | ||
devtobi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.