@@ -19,6 +19,8 @@ import org.junit.jupiter.api.Tag
1919import org.junit.jupiter.api.Test
2020import org.junit.jupiter.api.assertAll
2121import org.junit.jupiter.api.assertDoesNotThrow
22+ import org.junit.jupiter.api.assertInstanceOf
23+ import org.junit.jupiter.api.assertNotNull
2224import org.junit.jupiter.api.assertThrows
2325import org.junit.jupiter.api.assertTimeout
2426import org.junit.jupiter.api.assertTimeoutPreemptively
@@ -107,5 +109,29 @@ class KotlinAssertionsDemo {
107109 Thread .sleep(100 )
108110 }
109111 }
112+
113+ @Test
114+ fun `assertNotNull with a smart cast` () {
115+ val nullablePerson: Person ? = person
116+
117+ assertNotNull(nullablePerson)
118+
119+ // The compiler smart casts nullablePerson to a non-nullable object.
120+ // The safe call operator (?.) isn't required.
121+ assertEquals(person.firstName, nullablePerson.firstName)
122+ assertEquals(person.lastName, nullablePerson.lastName)
123+ }
124+
125+ @Test
126+ fun `assertInstanceOf with a smart cast` () {
127+ val maybePerson: Any = person
128+
129+ assertInstanceOf<Person >(maybePerson)
130+
131+ // The compiler smart casts maybePerson to a Person object,
132+ // allowing to access the Person properties.
133+ assertEquals(person.firstName, maybePerson.firstName)
134+ assertEquals(person.lastName, maybePerson.lastName)
135+ }
110136}
111137// end::user_guide[]
0 commit comments