Repository: JetBrains/kotlin
File: libraries/stdlib/jdk8/test/jvm/optionals/OptionalsTest.kt (Kotlin)

Code before:
import kotlin.test.*
import kotlin.jvm.optionals.*

class OptionalsTest {
    @Test
    fun getOrNull() {
        assertEquals("foo", Optional.of("foo").getOrNull())
        assertNull(Optional.empty<String>().getOrNull())
    }

    @Test
    fun getOrDefault() {
        assertEquals("foo", Optional.of("foo").getOrDefault("bar"))
        assertEquals("bar", Optional.empty<String>().getOrDefault("bar"))

        // Return type can be a supertype
        assertNull(Optional.empty<String>().getOrDefault(null))
        assertEquals(5.0, Optional.empty<Int>().getOrDefault<Number, Int>(5.0))
    }

    @Test
    fun getOrElse() {
        assertEquals("foo", Optional.of("foo").getOrElse { throw AssertionError() })
        assertEquals("bar", Optional.empty<String>().getOrElse { "bar" })

        assertFailsWith<IllegalStateException>(message = "ran") {
            Optional.empty<String>().getOrElse { throw IllegalStateException("ran") }
        }

        // Return type can be a supertype
        assertNull(Optional.empty<String>().getOrElse { null })
        assertEquals(5.0, Optional.empty<Int>().getOrElse<Number, Int> { 5.0 })
    }

    @Test
    fun optionalToCollection_presentAddsValue() {
        val dest = mutableListOf<CharSequence>()
        Optional.of("foo").toCollection(dest)
        assertEquals(listOf<CharSequence>("foo"), dest)
    }

    @Test
    fun optionalToCollection_emptyAddsNothing() {
        val dest = mutableListOf<String>()
        Optional.empty<String>().toCollection(dest)
        assertEquals(emptyList(), dest)
    }

    @Test
    fun optionalToList() {
        assertEquals(listOf("foo"), Optional.of("foo").toList())

Review comment (suggestion):
`message` parameter here doesn't make it checked that the exception caught has the specified message. Instead it is used as a message of an assertion error if the assertion doesn't pass. 
If you want to check properties of the expected exception, you can use the following approach: https://youtrack.jetbrains.com/issue/KT-27418#focus=Comments-27-3092129.0-0

Code after:
import kotlin.test.*
import kotlin.jvm.optionals.*

class OptionalsTest {
    @Test
    fun getOrNull() {
        assertEquals("foo", Optional.of("foo").getOrNull())
        assertNull(Optional.empty<String>().getOrNull())
    }

    @Test
    fun getOrDefault() {
        assertEquals("foo", Optional.of("foo").getOrDefault("bar"))
        assertEquals("bar", Optional.empty<String>().getOrDefault("bar"))

        // Return type can be a supertype
        assertNull(Optional.empty<String>().getOrDefault(null))
        assertEquals(5.0, Optional.empty<Int>().getOrDefault<Number, Int>(5.0))
    }

    @Test
    fun getOrElse() {
        assertEquals("foo", Optional.of("foo").getOrElse { throw AssertionError() })
        assertEquals("bar", Optional.empty<String>().getOrElse { "bar" })

        // Return type can be a supertype
        assertNull(Optional.empty<String>().getOrElse { null })
        assertEquals(5.0, Optional.empty<Int>().getOrElse<Number, Int> { 5.0 })
    }

    @Test
    fun getOrElse_propagatesException() {
        val e = assertFailsWith<IllegalStateException> {
            Optional.empty<String>().getOrElse { throw IllegalStateException("ran") }
        }
        assertEquals("ran", e.message)
    }

    @Test
    fun optionalToCollection_presentAddsValue() {
        val dest = mutableListOf<CharSequence>()
        Optional.of("foo").toCollection(dest)
        assertEquals(listOf<CharSequence>("foo"), dest)
    }

    @Test
    fun optionalToCollection_emptyAddsNothing() {
        val dest = mutableListOf<String>()
        Optional.empty<String>().toCollection(dest)
        assertEquals(emptyList(), dest)
    }