huehnerlady
08/17/2020, 11:20 AMdata class Test(val property1: String?, property2: Int?)
and I would like to check if that property1 and property2 are not null. But I would like this generic for any object, not this specific one.
I currently check, if the String of the object does not contain null, BUT this feels a bit hacky.
Any idea how I can do that better?Michael de Kaste
08/17/2020, 11:35 AMTest::class.memberProperties.all{ it.getter.call(yourInstanceOfTest) != null }
huehnerlady
08/17/2020, 12:05 PMhuehnerlady
08/17/2020, 1:22 PMfun shouldNotContainNullValues(tested: Any?) {
tested?: return
tested::class.memberProperties.all{ it.getter.call(tested) != null }
}
the ?
is a leftover from a more complicated version which tests the submembers of a class
This works fine for a string, and other things, but when I try to use javas URI class, I get an IllegalCallableAccessException:
shouldNotContainNullValues(<http://java.net|java.net>.URI.create("foo"))
ends up in
kotlin.reflect.full.IllegalCallableAccessException: java.lang.IllegalAccessException: class kotlin.reflect.jvm.internal.calls.CallerImpl$FieldGetter cannot access a member of class java.net.URI (in module java.base) with modifiers "private transient"
Any idea what I can do there?Michael de Kaste
08/17/2020, 1:39 PMTest::class.memberProperties.onEach { it.isAccessible = true }.all{ it.getter.call(yourInstanceOfTest) != null }
However, you can't just do this for all classes, for instance, if you look at the URI class you'll see fields like decodedPath and decodedQuery, which are all set to null.Michael de Kaste
08/17/2020, 1:39 PMhuehnerlady
08/17/2020, 3:16 PMJames Richardson
08/17/2020, 5:34 PMhuehnerlady
08/18/2020, 6:22 AMit.getter.call(yourInstanceOfTest)
returns a nullable so I need to have the whole function nullable unfortunatelyhuehnerlady
08/18/2020, 9:58 AMdata class Test(val property1: String?, val property2: SubTest?)
data class SubTest(val property3: URI?, val property4: Int?)
I would like to fail if any of those properties are null, but not if a subfield of URI is null, or other classes that are not part of my API 🤔
but if I do not do it recursively then TEST("foo", SubTest(null, 5)
would pass even though property3
is actually null 🤔
Any idea how I can do that? I assume this is the reason why there isn’t any method like this in kotest already 😓