-
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
base: main
Are you sure you want to change the base?
Changes from all commits
6ac0961
8bcf652
41d5b5c
6ba6a2b
429c843
2a308f4
564bca9
8988021
8cc94f3
8910444
a5fb268
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move this method all the way to the top because when we add a new rule this stream needs to be extended but the parameterized itself not so its less important. |
||
| 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)); | ||
|
Comment on lines
+34
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of duplicating the rule name I'd add a more descriptive name that describes the ArchUnit check |
||
| } | ||
|
|
||
| } | ||
| 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
|
||
|
|
||
| @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) | ||
|
Comment on lines
+21
to
+25
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most tests have these 4 lines in common, maybe you can move them into a reusable snippet? e.g. |
||
| .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$") | ||
| .allowEmptyShould(true); | ||
|
|
||
| public static final ArchRule RULE_AFTER_EACH_NAMING_CONVENTION_MATCHED = methods() | ||
| .that().areAnnotatedWith(AfterEach.class).should().haveNameMatching("^tearDown$") | ||
| .allowEmptyShould(true); | ||
|
|
||
| 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() | ||
| .areAnnotatedWith(TestTemplate.class).should() | ||
|
Comment on lines
+37
to
+40
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code duplication |
||
| .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() | ||
| .areAnnotatedWith(TestTemplate.class) | ||
|
Comment on lines
+46
to
+49
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code duplication |
||
| .should(haveTopEnclosingClassEndingWithTest); | ||
| } | ||
| 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
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.