Skip to content

Commit

Permalink
Junit4-support for @ArchTest-annotated non-static methods in abstract…
Browse files Browse the repository at this point in the history
… base class

Issue: #104

Signed-off-by: Manfred Hanke <[email protected]>
  • Loading branch information
hankem committed Aug 28, 2018
1 parent 1ebe38e commit d2e404a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void executeTestMethod(JavaClasses classes) {
"Methods annotated with @%s must have exactly one parameter of type %s",
ArchTest.class.getSimpleName(), JavaClasses.class.getSimpleName());

invokeMethod(testMethod, classes);
invokeMethod(testMethod, testClass, classes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public void should_allow_instance_field_in_abstract_base_class() {
verifyTestFinishedSuccessfully(AbstractBaseClass.INSTANCE_FIELD_NAME);
}

@Test
public void should_allow_non_static_method_in_abstract_base_class() {
ArchUnitRunner runner = newRunnerFor(ArchTestWithAbstractBaseClass.class, cache);

runner.runChild(ArchUnitRunnerTestUtils.getRule(AbstractBaseClass.NON_STATIC_METHOD_NAME, runner), runNotifier);

verifyTestFinishedSuccessfully(AbstractBaseClass.NON_STATIC_METHOD_NAME);
}

@Test
public void should_fail_on_wrong_field_type() {
ArchUnitRunner runner = newRunnerFor(WrongArchTestWrongFieldType.class, cache);
Expand Down Expand Up @@ -200,9 +209,15 @@ public static class ArchTestWithAbstractBaseClass extends AbstractBaseClass {

abstract static class AbstractBaseClass {
static final String INSTANCE_FIELD_NAME = "abstractBaseClassInstanceField";
static final String NON_STATIC_METHOD_NAME = "abstractBaseClassNonStaticMethod";

@ArchTest
ArchRule abstractBaseClassInstanceField = all(classes()).should(BE_SATISFIED);

@ArchTest
void abstractBaseClassNonStaticMethod(JavaClasses classes) {
all(classes()).should(BE_SATISFIED).check(classes);
}
}

@AnalyzeClasses(packages = "some.pkg")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public Type getType() {

@Override
public ArchUnitEngineExecutionContext execute(ArchUnitEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
invokeMethod(method, classes.get());
invokeMethod(method, method.getDeclaringClass(), classes.get());
return context;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ static <T> T getValueOrThrowException(Field field, Class<?> fieldOwner, Function
}
}

static <T> T invokeMethod(Method method, Object... args) {
static <T> T invokeMethod(Method method, Class<?> methodOwner, Object... args) {
if (Modifier.isStatic(method.getModifiers())) {
return invoke(null, method, args);
} else {
return invoke(newInstanceOf(method.getDeclaringClass()), method, args);
return invoke(newInstanceOf(methodOwner), method, args);
}
}

Expand Down

0 comments on commit d2e404a

Please sign in to comment.