Gefest
Gefest

Reputation: 123

Test if class extends specific class ArchUnit

Is there way to test that classes extends specific class in ArchUnit test?

I have 3 classes:

  1. ClassA
  2. ClassB which extends ClassA
  3. ClassC which extends ClassB

I need to validate that ClassC extends ClassA. The following test

ArchRuleDefinition.classes()
    .that()
        .haveSimpleName("ClassC")
    .should()
        .beAssignableTo("ClassA")
    .check(classes);

fails with violation error

Architecture Violation [Priority: MEDIUM] - Rule 'classes that have simple name 'ClassC' should be assignable to ClassA' was violated (1 times):
Class <ClassC> is not assignable to ClassA in ...

Upvotes: 0

Views: 1402

Answers (1)

Manfred
Manfred

Reputation: 3142

You can only use beAssignableTo("ClassA") if ClassA resides in the default package.

In general, you have to use fully qualified class name, i.e. ClassA.class.getName().

If ClassA is available on the classpath, I'd use beAssignableTo(ClassA.class).

Upvotes: 1

Related Questions