这个问题一般发生在使用 ArchUnit 进行单元测试时,当期望的 BaseType 和实际传递的 Type 不匹配时会出现。可能的解决方法是明确期望的 BaseType 类型并进行检查,同时检查实际传递的 Type 是否符合预期。下面是一个示例代码:
ArchCondition myCustomCondition = new ArchCondition("my custom condition") {
@Override
public void check(T item, ConditionEvents events) {
// Custom implementation
}
}.as("my custom condition");
ArchRule rule = classes().that().areAnnotatedWith(MyAnnotation.class)
.should().have(myCustomCondition);
JavaClasses classes = new ClassFileImporter().importPackages("my.example.package");
rule.check(classes);
在这个示例中,如果 T 类型不与 BaseType 匹配,则可能会抛出'ArchUnit base type thrown wrong”异常。为了解决这个问题,可以使用以下代码将 T 明确为预期的 BaseType 类型:
ArchCondition myCustomCondition = new ArchCondition("my custom condition") {
@Override
public void check(MyClassType item, ConditionEvents events) {
// Custom implementation
}
}.as("my custom condition");
ArchRule rule = classes().that().areAnnotatedWith(MyAnnotation.class)
.should().have(myCustomCondition);
JavaClasses classes = new ClassFileImporter().importPackages("my.example.package");
rule.check(classes);
在这个代码示例中,显式指定了 MyBaseType 类型为 MyClassType,从而避免了'ArchUnit base type thrown wrong”异常。