I’m trying to convert some tests from straight jUn...
# kotest
j
I’m trying to convert some tests from straight jUnit and need a bit of help. I have this
Copy code
fun <T> assertValidation(o: Any, validation: Class<T>, propertyName: String? = null) {

        val violations = when (propertyName) {
            null -> validator.validate(o)
            else -> validator.validateProperty(o, propertyName)
        }

        assertAll(
            Executable {
                assertEquals(
                    1,
                    violations.count(),
                    "Expecting one violation, received ${violations.count()}"
                )
            },
            Executable {
                violations.forEach { violation ->
                    val annotation = violation.constraintDescriptor.annotation
                    assertThat(annotation, instanceOf(validation))
                }
            }
        )
I’m trying to convert the type assertion to use ShouldBeInstanceOf<t> but the compiler doesn’t like it because its a generic type. I’m trying to change it to something along the lines of
Copy code
inline fun <reified T> assertValidation(o: Any, validation: Class<T>, propertyName: String? = null) {
        val violations = when (propertyName) {
            null -> validator.validate(o)
            else -> validator.validateProperty(o, propertyName)
        }

        assertSoftly {
            withClue("Expecting one violation, received ${violations.count()}") {
                violations.shouldHaveSize(1)
            }
            violations.forEach { violation ->
                val annotation = violation.constraintDescriptor.annotation
                annotation.shouldBeInstanceOf<validation>()
                //assertThat(annotation, instanceOf(validation))
            }
        }
    }
How can I use ShouldBeInstanceOf in this case?
s
You don't need to pass in the class<T>
because the type system is getting that from your type parameter to the function
Copy code
inline fun <reified T> assertValidation(o: Any, validation: Class<T>, propertyName: String? = null) {
        val violations = when (propertyName) {
            null -> validator.validate(o)
            else -> validator.validateProperty(o, propertyName)
        }

        assertSoftly {
            withClue("Expecting one violation, received ${violations.count()}") {
                violations.shouldHaveSize(1)
            }
            violations.forEach { violation ->
                val annotation = violation.constraintDescriptor.annotation
                annotation.shouldBeInstanceOf<T>()
                //assertThat(annotation, instanceOf(validation))
            }
        }
    }
j
I tried that, but I get this:
Copy code
Type argument is not within its bounds: should be subtype of 'Any'
s
Copy code
inline fun <reified T: Any>
^^ change to that
👍 1
j
Thanks @sam!
s
You're welcome