Skip to main content

Captured Outputs

CapturedOutputs is a thread-safe store for values produced during the whenever step — HTTP responses, returned objects, database query results, etc. Stored values are available in then for assertions and appear in the Captured Outputs tab of the HTML report.


Typed Keys​

The preferred approach is to define a typed key upfront. This gives you compile-time safety on both put and get.

// Define once, e.g. as a companion object field or top-level val
val response = capturedOutput<HttpResponse>("response")

class PaymentTest : KensaTest, WithKotest {

@Test
fun `payment is accepted`() {
given { /* ... */ }
whenever { ctx ->
ctx.outputs[response] = paymentGateway.submit(ctx.fixtures[paymentRequest])
}
then({ ctx -> ctx.outputs[response] }) {
statusCode shouldBe 200
}
}
}

String Keys​

For quick, untyped access — useful when the value type is obvious from context:

whenever { ctx ->
ctx.outputs.put("response", httpClient.post("/payments", body))
}
then({ ctx -> ctx.outputs["response"] as HttpResponse }) {
statusCode shouldBe 200
}

API Reference​

capturedOutput<T>() factory (Kotlin)​

inline fun <reified T : Any> capturedOutput(
key: String,
highlighted: Boolean = false
): CapturedOutput<T>

createCapturedOutput() factory (Java)​

createCapturedOutput(String key, Class<T> type)
createCapturedOutput(String key, Class<T> type, boolean highlighted)

CapturedOutputs methods​

MethodDescription
put(key: String, value: Any)Store by string key
outputs[key] = value / set(key: CapturedOutput<T>, value: T)Store by typed key
outputs[key] / get(key)Retrieve by string key (throws if absent)
outputs[key] / get(key: CapturedOutput<T>)Retrieve by typed key (throws if absent)
getOrNull(key)Retrieve by string or typed key, returns null if absent
contains(key)Check for presence by string or typed key
values()All stored values as Set<NamedValue>
highlightedValues()Only highlighted values

Highlighting​

Highlighted outputs appear prominently in the report. Pass highlighted = true when creating the key:

val transactionId = capturedOutput<String>("transaction id", highlighted = true)

whenever { ctx ->
val result = paymentGateway.submit(ctx.fixtures[paymentRequest])
ctx.outputs[transactionId] = result.transactionId
}