willowtreeapps / assertk

assertions for kotlin inspired by assertj
MIT License
757 stars 84 forks source link

assertFailsWith<SomeExceptionType> { ... } #511

Open swankjesse opened 7 months ago

swankjesse commented 7 months ago

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.