UseIndexAssertion¶
Issue ID UseIndexAssertion ·
Severity Warning ·
Category Correctness ·
Default on
Flags assertThat(collection[i]) and assertThat(array[i]) where assertk's
index (and friends like first()) should be used.
Why¶
Indexing the collection inside assertThat throws an
IndexOutOfBoundsException at the assertion site — the failure looks
like a crash, not an assertion failure. Assert<List<T>>.index(i): Assert<T>
asserts that the element exists and transforms the subject so you can chain
further checks on the value.
The detector recognises both Array and java.util.List (and subtypes) as
indexable. Reads through index that are not the assertThat argument —
e.g. assertThat(array[1].sign).isEqualTo(1) — are intentionally not
flagged.
Example¶
Quick fix¶
Yes — the IDE rewrites
assertThat(array[i]) to assertThat(array).index(i) and adds the
assertk.assertions.index import.