assertk icon indicating copy to clipboard operation
assertk copied to clipboard

assertFailsWith<SomeExceptionType> { ... }

Open swankjesse opened this issue 7 months ago • 0 comments

This PR migrated about 500 exception tests in OkHttp from this form:

try {
  doThing()
  fail()
} catch (expected: SomeSpecificExceptionType) {
}

to this form:

assertFailsWith<SomeSpecificExceptionType> {
  doThing()
}

I had to use kotlin.test’s assertFailsWith() even though I generally prefer assertk’s APIs, because assertk doesn’t have a function that lets me expect a failure of a specific type in one go.

I know I could have done this:

assertFailure {
  doThing()
}.isInstanceOf<SomeSpecificExceptionType>()

But it’s more code, and I’m in the code deleting business.

swankjesse avatar Jan 06 '24 05:01 swankjesse