In the absence of
shouldContainOnly
Kotest can be used only with workarounds, such as by making changes to the actual value to be tested. Compare AssertJ's
containsOnly
with attempts to use Kotest:
package org.example
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.collections.*
import org.junit.jupiter.api.Test
import org.assertj.core.api.Assertions.*
class KotlinPracticeTest {
@Test
fun foo() {
// AssertJ's containsOnly method is what is required
assertThat(listOf(1, 1, 1)).containsOnly(1)
assertThat(listOf(1, 1, 2, 2)).containsOnly(1, 2)
shouldThrow<AssertionError> { assertThat(listOf(1, 1, 2)).containsOnly(1, 2, 3) }
// There is no Kotest method to do the same thing:
// it only works with workarounds such as converting the actual value to be tested
listOf(1, 1, 1).toSet().shouldContainExactlyInAnyOrder(1)
listOf(1, 1, 2, 2).toSet().shouldContainExactlyInAnyOrder(1, 2) //fails
shouldThrow<AssertionError> { listOf(1, 1, 2).toSet().shouldContainExactlyInAnyOrder(1) }
}
}