Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contracts to Kotlin-specific assertions #3259

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ JUnit repository on GitHub.
- A section containing JUnit-specific metadata about each test/container to the HTML
report is now written by open-test-reporting when added to the classpath/module path
- Information about published files is now included as attachments.
* Introduced contracts for Kotlin-specific assertion methods.


[[release-notes-5.12.0-M1-junit-jupiter]]
Expand Down
26 changes: 26 additions & 0 deletions documentation/src/test/kotlin/example/KotlinAssertionsDemo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertInstanceOf
import org.junit.jupiter.api.assertNotNull
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.assertTimeout
import org.junit.jupiter.api.assertTimeoutPreemptively
Expand Down Expand Up @@ -107,5 +109,29 @@ class KotlinAssertionsDemo {
Thread.sleep(100)
}
}

@Test
fun `assertNotNull with a smart cast`() {
val nullablePerson: Person? = person

assertNotNull(nullablePerson)

// The compiler smart casts nullablePerson to a non-nullable object.
// The safe call operator (?.) isn't required.
assertEquals(person.firstName, nullablePerson.firstName)
assertEquals(person.lastName, nullablePerson.lastName)
}

@Test
fun `assertInstanceOf with a smart cast`() {
val maybePerson: Any = person

assertInstanceOf<Person>(maybePerson)

// The compiler smart casts maybePerson to a Person object,
// allowing to access the Person properties.
assertEquals(person.firstName, maybePerson.firstName)
assertEquals(person.lastName, maybePerson.lastName)
}
}
// end::user_guide[]
Loading