robfletcher / strikt

An assertion library for Kotlin
https://strikt.io/
Apache License 2.0
558 stars 61 forks source link

Should MappingFailed extend AssertionFailedError? #269

Open robd opened 2 years ago

robd commented 2 years ago

Currently if some code wants to handle assertion failures, this doesn't work for strikt helpers like first() because MappingException extends IncompleteExecutionException, not AssertionError. eg:

  private val failures = mutableListOf<AssertionError>()

  fun doSomethingWithFailures(runnable: Runnable) {
    try {
      runnable.run()
    } catch(assertionFailed: AssertionError) {
      failures.add(assertionFailed)
      throw assertionFailed
    }
  }

  private fun thingUnderTest() = emptyList<String>()

  // failure will be added to failures
  @Test
  fun `thingUnderTest has a catflap`() {
    doSomethingWithFailures {
      expectThat(thingUnderTest()).contains("catflap")
    }
  }

  // failure will not be added to failures
  @Test
  fun `thingUnderTest has a catflap first`() {
    doSomethingWithFailures {
      expectThat(thingUnderTest()).first().isEqualTo("catflap")
    }
  }

Would it be better to have MappingException extend org.opentest4j.AssertionFailedError instead?