How would you write assertThat(foo, instanceOf(Bar.class))
with Kotlin?
Seems that it does not like the .class
如果可能的话,我想要一个比assertTrue(foo is Bar)
更"精确"的断言.
How would you write assertThat(foo, instanceOf(Bar.class))
with Kotlin?
Seems that it does not like the .class
如果可能的话,我想要一个比assertTrue(foo is Bar)
更"精确"的断言.
Bar::class
返回KClass
的实例,这是相当于Java Class
的Kotlin.
instanceOf
方法需要Class
个实例,而不是KClass
个,所以您必须使用Bar::class.java
来转换它.
所以你的断言应该是这样的:
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>())