Kotlin for assertThat(foo, instanceOf(Bar.class))

kotlin instanceof
kotlin hamcrest
hamcrest instanceof
kotlin activity instance of
instanceof in kotlin example
kotlin check if class implements interface
context instanceof activity kotlin
assertj kotlin

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>())

The signature of isA is completely wrong · Issue #27 · hamcrest , class hierarchy Foo extends Bar. Bar thing = somemethod(); assertThat(thing, isA​(Foo.class)) isA is not an equivalent to is(instanceOf()) #39. Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

assertThat(foo, instanceOf(Bar::class.java))

Documentation: https://kotlinlang.org/docs/reference/java-interop.html#getclass

Object Expressions, Object Declarations and Companion , To create an object of an anonymous class that inherits from some type (or types)​, we write: fun bar() { val x1 = foo().x // Works val x2 = publicFoo().x // ERROR:  Kotlin для assertThat (foo, instanceOf (Bar.class)) Kotlin Back-Tick в именах методов: как это работает? Запуск тестов юнита, написанных в Котлине с аннотацией @category

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) 
}

org.hamcrest.Matchers.isA java code examples, assertThat(objects, isA(Iterable.class)); Best Java code snippets using org.​hamcrest. A shortcut to the frequently used is(instanceOf(SomeClass.class)) . Kotlin для assertThat (foo, instanceOf (Bar.class)) Тип-Безопасность с необязательными полями в классе данных для JSON Почему Котлин не поддерживает «тройного оператора»,

How to use JUnit 5 @MethodSource-parameterized tests with Kotlin , @ParameterizedTest. @ValueSource (strings = arrayOf( "foo" , "bar" )). fun `test isBlank() works as expected`(testedValue: String) {. Assertions. Являются ли входящие функции Kotlin менее дорогими, чем анонимные классы Java? Kotlin: java.lang.UnsupportedOperationException в элементе добавления MutableList; Kotlin для assertThat (foo, instanceOf (Bar.class))

Micronaut Test, @MockBean - Can be added to methods or inner classes of a test class to define for Java or Kotlin (this should already be configured if you used mn create-app ). num, Integer square) { when(mathService.compute(10)) .then(invocation -> Long. instanceOf(MyServiceStuff.class)); } @Property(name = "foo.bar", value  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

HasPropertyWithValue (Hamcrest), public class HasPropertyWithValue<T>; extends TypeSafeDiagnosingMatcher<T​>. Matcher that asserts that a JavaBean property on an argument passed to the  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Comments
  • AssertJ is btw a nice fluent api for assertions. It would be written like this: assertThat(foo).isInstanceOf(Bar::class.java). The big advantage is, that you must not know all the components like instanceof() by hard, because the code completion of the fluent-api lists you all possibilities.
  • That's not completely true: Bar.class isn't allowed in kotlin. It is Bar::class what you mean
  • Thanks for the Hamkrest link
  • Too bad there ain't an assertIs(foo, Bar) function :/ Thanks :)