How would you write assertThat(foo, instanceOf(Bar.class))
with Kotlin?
Seems that it does not like the .class
I would like to go for an assertion which is a bit more "precise" than just an assertTrue(foo is Bar)
if possible
Bar::class
returns instance of KClass
, which is Kotlin equivalent of Java's Class
.
instanceOf
method requires Class
instance, not KClass
, so you have to convert it using Bar::class.java
.
So your assertion should be like:
assertThat(foo, instanceOf(Bar::class.java))
More info about Java interop you can find here.
Also you can have a look at Hamkrest library which may add more fluency to your assertions:
assert.that(foo, isA<Bar>())
You could use Atrium (https://atriumlib.org) and write the following
assertThat(foo).isA<Bar>{}
And in case you want to assert more about foo, say Bar has a val baz: Int
, then you can write the following
assertThat(foo).isA<Bar> {
property(subject::baz).isSmallerThan(41)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With