Best Kotest code snippet using com.sksamuel.kotest.throwablehandling.CovariantThrowableHandlingTest.verifyThrowsExactly
CovariantThrowableHandlingTest.kt
Source:CovariantThrowableHandlingTest.kt
...111 "Should throw the thrown exception" - {112 "When it's not an instance nor subclass of the right class" {113 val thrownException = FooRuntimeException()114 onShouldNotThrowMatcher<ParentException> { shouldNotThrowMatcher ->115 verifyThrowsExactly(thrownException) {116 shouldNotThrowMatcher { throw thrownException }117 }118 }119 }120 "When it's an instance of the parent class of the right class" {121 val thrownException = ParentException()122 onShouldNotThrowMatcher<SubException> { shouldNotThrowMatcher ->123 verifyThrowsExactly(thrownException) {124 shouldNotThrowMatcher { throw thrownException }125 }126 }127 }128 }129 "Should not throw an exception" - {130 "When no exception is thrown" {131 onShouldNotThrowMatcher<ParentException> { shouldNotThrowMatcher ->132 val thrown = catchThrowable {133 shouldNotThrowMatcher { /* Nothing thrown */ }134 }135 thrown shouldBe null136 }137 }138 }139 }140 "should throw with message" - {141 "When the correct exception is thrown, but the message is wrong" {142 onShouldThrowWithMessageMatcher<Exception>("foo") { shouldThrowMatcher ->143 verifyThrowsWrongExceptionMessage("foo", "bar") {144 shouldThrowMatcher { throw Exception("bar") }145 }146 }147 }148 "Exception class type should have priority in assertion" {149 val instanceToThrow = Exception("foo")150 runCatching {151 shouldThrowWithMessage<RuntimeException>("bar") {152 throw instanceToThrow153 }154 }155 .exceptionOrNull() shouldBe AssertionError("Expected exception java.lang.RuntimeException but a Exception was thrown instead.")156 }157 }158 }159 private fun <T> onShouldThrowWithMessageMatcher(message: String, func: (ShouldThrowMatcher<T>) -> Unit) {160 func { shouldThrowUnitWithMessage(message, it) }161 func { shouldThrowWithMessage(message, it) }162 }163 private inline fun <reified T : Throwable> onShouldThrowMatcher(func: (ShouldThrowMatcher<T>) -> Unit) {164 func(::shouldThrowUnit)165 func { shouldThrow(it) }166 }167 private fun verifyNoExceptionThrownError(expectedClass: KClass<*>, block: () -> Unit) {168 val throwable = catchThrowable(block)169 throwable.shouldBeInstanceOf<AssertionError>()170 throwable.message shouldBe "Expected exception ${expectedClass.qualifiedName} but no exception was thrown."171 }172 private fun verifyThrowsAssertionErrorInstance(assertionErrorInstance: AssertionError, block: () -> Unit) {173 val throwable = catchThrowable(block)174 (throwable === assertionErrorInstance).shouldBeTrue()175 }176 private fun verifyThrowsWrongExceptionClass(177 thrownInstance: Throwable,178 expectedClass: KClass<*>,179 incorrectClass: KClass<*>,180 block: () -> Unit181 ) {182 val throwable = catchThrowable(block)183 throwable.shouldBeInstanceOf<AssertionError>()184 throwable.message shouldBe "Expected exception ${expectedClass.qualifiedName} but a ${incorrectClass.simpleName} was thrown instead."185 (throwable.cause === thrownInstance).shouldBeTrue()186 }187 private fun verifyThrowsWrongExceptionMessage(188 expectedMessage: String,189 actualMessage: String,190 block: () -> Unit191 ) {192 val throwable = catchThrowable(block)193 throwable.shouldBeInstanceOf<AssertionError>()194 throwable.message shouldBe "Expected exception message $expectedMessage but was $actualMessage instead."195 }196 private fun verifyReturnsExactly(thrownException: Throwable, block: () -> Any?) {197 val actualReturn = block()198 (thrownException === actualReturn).shouldBeTrue()199 }200 private inline fun <reified T : Throwable> onShouldNotThrowMatcher(func: (ShouldNotThrowMatcher<T>) -> Unit) {201 func { shouldNotThrowUnit<T> { it() } }202 func { shouldNotThrow<T>(it) }203 }204 private fun verifyThrowsAssertionWrapping(exception: Throwable, block: () -> Unit) {205 val thrown = catchThrowable(block)206 thrown.shouldBeInstanceOf<AssertionError>()207 thrown.message shouldBe "No exception expected, but a ${exception::class.simpleName} was thrown."208 thrown.cause shouldBeSameInstanceAs exception209 }210 private fun verifyThrowsExactly(exception: Throwable, block: () -> Unit) {211 val thrown = catchThrowable(block)212 thrown shouldBeSameInstanceAs exception213 }214}215private typealias ShouldThrowMatcher<T> = (() -> Unit) -> T216private typealias ShouldNotThrowMatcher<T> = (() -> Unit) -> Unit...
verifyThrowsExactly
Using AI Code Generation
1import io.kotest.assertions.throwablehandling.shouldThrowExactly2import io.kotest.core.spec.style.FunSpec3import io.kotest.matchers.shouldBe4import java.io.IOException5class CovariantThrowableHandlingTest : FunSpec({6 test("shouldThrowExactly should match the expected exception") {7 shouldThrowExactly<IOException> {8 throw IOException("boom")9 }10 }11 test("shouldThrowExactly should fail if the expected exception is not thrown") {12 shouldThrowExactly<IOException> {13 throw IllegalArgumentException("boom")14 }15 }16 test("shouldThrowExactly should match the expected exception and message") {17 shouldThrowExactly<IOException>("boom") {18 throw IOException("boom")19 }20 }21 test("shouldThrowExactly should fail if the expected exception is not thrown with the expected message") {22 shouldThrowExactly<IOException>("boom") {23 throw IOException("boom2")24 }25 }26 test("shouldThrowExactly should fail if the expected exception is not thrown with a message that contains the expected message") {27 shouldThrowExactly<IOException>("boom") {28 throw IOException("boom2")29 }30 }31 test("shouldThrowExactly should match the expected exception and message using a matcher") {32 shouldThrowExactly<IOException> { e ->33 }34 }35 test("shouldThrowExactly should fail if the expected exception is not thrown with the expected message using a matcher") {36 shouldThrowExactly<IOException> { e ->37 }38 }39 test("shouldThrowExactly should fail if the expected exception is not thrown with a message that contains the expected message using a matcher") {40 shouldThrowExactly<IOException> { e ->41 }42 }43 test("shouldThrowExactly should match the expected exception and message using a string matcher") {44 shouldThrowExactly<IOException>("boom") {45 throw IOException("boom")46 }47 }48 test("shouldThrowExactly should fail if the expected exception is not thrown with the expected message using a string matcher") {49 shouldThrowExactly<IOException>("boom") {50 throw IOException("boom2")51 }52 }53 test("shouldThrowExactly should fail if the expected exception is not thrown with
verifyThrowsExactly
Using AI Code Generation
1 fun `verifyThrowsExactly should throw an exception if the given block does not throw an exception`() {2 val block: () -> Unit = {}3 val exception = shouldThrowExactly<AssertionError> {4 verifyThrowsExactly<Throwable>(block)5 }6 }7 fun `verifyThrowsExactly should throw an exception if the given block throws an exception of a different type`() {8 val block: () -> Unit = { throw IllegalArgumentException() }9 val exception = shouldThrowExactly<AssertionError> {10 verifyThrowsExactly<Throwable>(block)11 }12 }13 fun `verifyThrowsExactly should not throw an exception if the given block throws an exception of the given type`() {14 val block: () -> Unit = { throw Throwable() }15 verifyThrowsExactly<Throwable>(block)16 }17 fun `verifyThrowsExactly should throw an exception if the given block does not throw an exception`() {18 val block: () -> Unit = {}19 val exception = shouldThrowExactly<AssertionError> {20 verifyThrowsExactly<Throwable>(block)21 }22 }23 fun `verifyThrowsExactly should throw an exception if the given block throws an exception of a different type`() {24 val block: () -> Unit = { throw IllegalArgumentException() }25 val exception = shouldThrowExactly<AssertionError> {26 verifyThrowsExactly<Throwable>(block)27 }28 }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!