TryCatchAssertion¶
Issue ID TryCatchAssertion ·
Severity Warning ·
Category Correctness ·
Default on
Flags simple try { ... } catch (e: Exception) { assertThat(e)... } blocks
whose only purpose is to assert on an expected exception. Use
assertk.assertFailure { ... } instead.
Why¶
A try/catch whose catch body only contains assertThat(e) calls is
fragile: if you forget to put a fail() after the throwing call (or the call
silently doesn't throw), the test passes when it shouldn't. assertFailure
inverts the contract — the block must throw, and you assert on the
captured Throwable directly.
The detector is intentionally conservative. It only fires when:
- the
tryblock has exactly one non-fail()call, and - the single
catchclause's body contains only assertkassertThat(...)chains.
Anything else (multiple statements, mixed work, multiple catches) is left alone because the block likely exists for reasons beyond exception assertion.
Example¶
Quick fix¶
Not provided — the rewrite is structural (delete the try/catch,
extract the throwing call, hoist the catch-body assertions onto an
assertFailure chain) and depends on details that can't always be inferred
mechanically.