I have a kotlin object like so;
data class Car(
val id: UUID
)
the id is not nullable thanks to kotlin compile time checks. I just want to make sure that in the future, a test will fail if anyone changes the line to
val id: UUID?
because that field being nullable will mess up several services that depend on id (save/get/etc). I was thinking i could make another Car-like class with a nullable id, and write tests for my services to make sure they will handle null ids, but its not super easy, and it seems wrong.
My other option seems to be accessing an instance method of the id in test, like so
@Test
fun `dont change Car.id to nullable`(){
Car().id.timestamp() // calling timestamp() will no longer compile if someone changes UUID to UUID?
}
but this seems confusing
I look at tests as though they protect the code. So if anyone comes in they should be able to change anything they want, and as long as they dont break any existing tests, they can be confident that they didnt break any existing functionality.
But i dont know how to stop someone from changing "UUID" to "UUID?", which would introduce a huge bug
If you are ok with using reflection, then you could do this:
assertFalse(Car::id.returnType.isMarkedNullable)