CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-kotest--kotest-assertions-core-jvm

Core assertion and matcher library for Kotest testing framework

Pending
Overview
Eval results
Files

throwable.mddocs/

Throwable Matchers

Comprehensive throwable and exception validation including message checks, cause analysis, stack trace validation, and type assertions for robust error testing capabilities.

Capabilities

Message Validation

Assert that throwables have specific message content.

/**
 * Assert that this throwable has the expected message (exact match)
 * @param message The expected exact message content
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldHaveMessage(message: String): Throwable

/**
 * Assert that this throwable does not have the specified message
 * @param message The message that should not be present
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldNotHaveMessage(message: String): Throwable

/**
 * Assert that this throwable has a message matching the regex pattern
 * @param message The regex pattern to match against the message
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldHaveMessage(message: Regex): Throwable

/**
 * Assert that this throwable does not have a message matching the regex
 * @param message The regex pattern that should not match
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldNotHaveMessage(message: Regex): Throwable

Usage Examples:

import io.kotest.matchers.throwable.shouldHaveMessage
import io.kotest.matchers.throwable.shouldNotHaveMessage

// Exact message validation
val exception = IllegalArgumentException("Invalid input provided")
exception shouldHaveMessage "Invalid input provided"
exception shouldNotHaveMessage "Different message"

// Regex message validation
exception shouldHaveMessage Regex("Invalid .* provided")
exception shouldNotHaveMessage Regex("Valid .*")

// Testing caught exceptions
shouldThrow<IllegalArgumentException> {
    processInput("")
} shouldHaveMessage "Input cannot be empty"

Cause Analysis

Assert and validate exception causes in the throwable chain.

/**
 * Assert that this throwable has a cause and optionally validate it
 * @param block Optional validation block for the cause
 * @return The original Throwable for chaining
 */
fun Throwable.shouldHaveCause(block: (Throwable) -> Unit = {}): Throwable

/**
 * Assert that this throwable does not have a cause
 * @return The original Throwable for chaining
 */
fun Throwable.shouldNotHaveCause(): Throwable

/**
 * Assert that this throwable has a cause of the specified type or subtype
 * @return The original Throwable for chaining
 */
inline fun <reified T : Throwable> Throwable.shouldHaveCauseInstanceOf(): Throwable

/**
 * Assert that this throwable does not have a cause of the specified type
 * @return The original Throwable for chaining
 */
inline fun <reified T : Throwable> Throwable.shouldNotHaveCauseInstanceOf(): Throwable

/**
 * Assert that this throwable has a cause of exactly the specified type
 * @return The original Throwable for chaining
 */
inline fun <reified T : Throwable> Throwable.shouldHaveCauseOfType(): Throwable

/**
 * Assert that this throwable does not have a cause of exactly the specified type
 * @return The original Throwable for chaining
 */
inline fun <reified T : Throwable> Throwable.shouldNotHaveCauseOfType(): Throwable

Usage Examples:

import io.kotest.matchers.throwable.shouldHaveCause
import io.kotest.matchers.throwable.shouldNotHaveCause
import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf
import io.kotest.matchers.throwable.shouldHaveCauseOfType

// Basic cause validation
val cause = IOException("Network error")
val wrapper = RuntimeException("Operation failed", cause)

wrapper.shouldHaveCause()
wrapper.shouldHaveCause { it shouldHaveMessage "Network error" }

// Type-specific cause validation  
wrapper.shouldHaveCauseInstanceOf<IOException>()
wrapper.shouldHaveCauseOfType<IOException>()

// Exception without cause
val simple = IllegalStateException("Simple error")
simple.shouldNotHaveCause()

Stack Trace Validation

Assert that stack traces contain specific content or patterns.

/**
 * Assert that this throwable's stack trace contains the substring
 * @param substr The substring to search for in the stack trace
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldHaveStackTraceContaining(substr: String): Throwable

/**
 * Assert that this throwable's stack trace does not contain the substring
 * @param substr The substring that should not be in the stack trace
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldNotHaveStackTraceContaining(substr: String): Throwable

/**
 * Assert that this throwable's stack trace matches the regex pattern
 * @param regex The regex pattern to match against the stack trace
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldHaveStackTraceContaining(regex: Regex): Throwable

/**
 * Assert that this throwable's stack trace does not match the regex
 * @param regex The regex pattern that should not match
 * @return The original Throwable for chaining
 */
infix fun Throwable.shouldNotHaveStackTraceContaining(regex: Regex): Throwable

Usage Examples:

import io.kotest.matchers.throwable.shouldHaveStackTraceContaining
import io.kotest.matchers.throwable.shouldNotHaveStackTraceContaining

// Stack trace content validation
val exception = RuntimeException("Test error")
exception shouldHaveStackTraceContaining "RuntimeException"
exception shouldHaveStackTraceContaining "Test error"
exception shouldNotHaveStackTraceContaining "SomeOtherClass"

// Regex pattern matching in stack trace
exception shouldHaveStackTraceContaining Regex("at .*\\.kt:\\d+")
exception shouldNotHaveStackTraceContaining Regex("at some\\.non\\.existent")

// Testing for specific method calls in stack
shouldThrow<Exception> {
    someMethodThatThrows()
} shouldHaveStackTraceContaining "someMethodThatThrows"

Common Exception Testing Patterns

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.throwable.*

// Comprehensive exception validation
val exception = shouldThrow<IllegalArgumentException> {
    validateInput("")
}

exception shouldHaveMessage "Input cannot be empty"
exception.shouldNotHaveCause()
exception shouldHaveStackTraceContaining "validateInput"

// Nested exception validation
val nested = shouldThrow<ProcessingException> {
    processData("invalid")
}

nested shouldHaveMessage "Failed to process data"
nested.shouldHaveCause { cause ->
    cause shouldHaveMessage "Invalid data format"
    cause.shouldHaveCauseInstanceOf<IllegalArgumentException>()
}

// Regex-based message validation for dynamic content
val timestampException = shouldThrow<RuntimeException> {
    generateTimestampedError()
}

timestampException shouldHaveMessage Regex("Error at \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}")

Type Definitions

interface Matcher<T> {
    fun test(value: T): MatcherResult
}

data class MatcherResult(
    val passed: Boolean,
    val failureMessage: () -> String,
    val negatedFailureMessage: () -> String
)

data class ComparableMatcherResult(
    val passed: Boolean, 
    val failureMessage: () -> String,
    val negatedFailureMessage: () -> String,
    val actual: String,
    val expected: String
) : MatcherResult

Install with Tessl CLI

npx tessl i tessl/maven-io-kotest--kotest-assertions-core-jvm

docs

collections.md

concurrency.md

core-dsl.md

datetime.md

filesystem.md

index.md

nondeterministic.md

primitives.md

reflection.md

result.md

strings.md

throwable.md

tuples.md

types.md

tile.json