https://kotlinlang.org logo
#konsist
Title
# konsist
t

Tomas Alabes

11/10/2023, 10:20 AM
Hey, I've been trying konsist and archunit for a couple of days and I've found konsist to have a way nicer api to write the tests. But I always end up giving up at the assertion phase. A couple of examples:
Copy code
@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:
Copy code
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):
Copy code
@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")
                    }
                }
            }
    }
i

igor.wojda

11/13/2023, 11:32 PM
@Tomas Alabes 1. Indeed this is not possible - I will take a look 2. Please post example of code that violates this RUle