Skip to content

Commit

Permalink
add JUnit5-support for @ArchTest-annotated instance methods in abstra…
Browse files Browse the repository at this point in the history
…ct base class

This corresponds to 625c3d6,
which added the same feature for JUnit4 (Issue: #104).

Signed-off-by: Manfred Hanke <[email protected]>
  • Loading branch information
hankem authored and codecholeric committed Jun 16, 2022
1 parent 7949155 commit f3ea378
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand Down Expand Up @@ -197,7 +198,10 @@ public Type getType() {

@Override
public ArchUnitEngineExecutionContext execute(ArchUnitEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
invokeMethod(method, method.getDeclaringClass(), classes.get());
Class<?> methodOwner = getParent().flatMap(parent ->
parent instanceof ArchUnitTestDescriptor ? Optional.of(((ArchUnitTestDescriptor) parent).testClass) : Optional.<Class<?>>empty()
).orElseGet(method::getDeclaringClass);
invokeMethod(method, methodOwner, classes.get());
return context;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.tngtech.archunit.junit.internal.testexamples.TestMethodWithTags;
import com.tngtech.archunit.junit.internal.testexamples.UnwantedClass;
import com.tngtech.archunit.junit.internal.testexamples.abstractbase.ArchTestWithAbstractBaseClassWithFieldRule;
import com.tngtech.archunit.junit.internal.testexamples.abstractbase.ArchTestWithAbstractBaseClassWithMethodRule;
import com.tngtech.archunit.junit.internal.testexamples.ignores.IgnoredClass;
import com.tngtech.archunit.junit.internal.testexamples.ignores.IgnoredField;
import com.tngtech.archunit.junit.internal.testexamples.ignores.IgnoredLibrary;
Expand Down Expand Up @@ -831,6 +832,17 @@ void a_simple_rule_method_with_violation() {
testListener.verifyViolation(simpleRuleMethodTestId(engineId), UnwantedClass.CLASS_VIOLATING_RULES.getSimpleName());
}

@Test
void instance_method_rule_in_abstract_base_class() {
simulateCachedClassesForTest(ArchTestWithAbstractBaseClassWithMethodRule.class, UnwantedClass.CLASS_SATISFYING_RULES);

EngineExecutionTestListener testListener = execute(engineId, ArchTestWithAbstractBaseClassWithMethodRule.class);

testListener.verifySuccessful(engineId
.append(CLASS_SEGMENT_TYPE, ArchTestWithAbstractBaseClassWithMethodRule.class.getName())
.append(METHOD_SEGMENT_TYPE, ArchTestWithAbstractBaseClassWithMethodRule.INSTANCE_METHOD_NAME));
}

@Test
void rule_library_without_violation() {
simulateCachedClassesForTest(SimpleRuleLibrary.class, UnwantedClass.CLASS_SATISFYING_RULES);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tngtech.archunit.junit.internal.testexamples.abstractbase;

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.junit.internal.testexamples.RuleThatFails;
import com.tngtech.archunit.junit.internal.testexamples.UnwantedClass;

public abstract class AbstractBaseClassWithMethodRule {
public static final String INSTANCE_METHOD_NAME = "abstractBaseClassInstanceMethod";

@ArchTest
void abstractBaseClassInstanceMethod(JavaClasses classes) {
RuleThatFails.on(UnwantedClass.CLASS_VIOLATING_RULES).check(classes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tngtech.archunit.junit.internal.testexamples.abstractbase;

import com.tngtech.archunit.junit.AnalyzeClasses;

@AnalyzeClasses(packages = "some.dummy.package")
public class ArchTestWithAbstractBaseClassWithMethodRule extends AbstractBaseClassWithMethodRule {
}

0 comments on commit f3ea378

Please sign in to comment.