Tomas Alabes
11/10/2023, 10:20 AM@Test
fun `methods annotated with @Cacheable should return Serializable types`() {
Konsist
.scopeFromPackage("com.archtests.cache")
.functions()
.withAnnotationOf(Cacheable::class)
.assertTrue {
it.hasToImplement(Serializable::class) // hasReturnType isn't what I need, and the lambda doesn't have anything
}
}
In archunit I do it like this:
val rule: ArchRule = methods()
.that().areAnnotatedWith(Cacheable::class.java)
.should().haveRawReturnType(object: DescribedPredicate<JavaClass>("should implement Serializable") {
override fun test(returnType: JavaClass): Boolean {
return returnType.isAssignableTo(Serializable::class.java)
}
})
Or in this example I can't type the assertions (I tried to use the types as much as possible):
@Test
fun noJsonTypeInfoShouldUseClassId() {
Konsist
.scopeFromPackage("com.archtests.jackson")
.interfaces()
.withAnnotationOf(JsonTypeInfo::class)
.assertTrue {
it.hasAnnotation { annotation ->
annotation.hasNameMatching(Regex(JsonTypeInfo::class.simpleName!!))
annotation.hasArgument { arg ->
arg.hasNameMatching(Regex("use"))
arg.hasValue("JsonTypeInfo.Id.NAME")
}
}
}
}
igor.wojda
11/13/2023, 11:32 PM