Skip to content

Instantly share code, notes, and snippets.

@jiffle
Last active March 2, 2023 15:44
Show Gist options
  • Save jiffle/9ca3db46f40283c06c294079095afa8b to your computer and use it in GitHub Desktop.
Save jiffle/9ca3db46f40283c06c294079095afa8b to your computer and use it in GitHub Desktop.
Strikt Cheatsheet

Tricks and tips for using Strikt assertions

Strikt is very powerful, however it is sometimes hard to find particular methods. This is a handy cheatsheet.

Basics

Top-level assertions

expect, expectThat, expectCatching and expectThrows

Narrowing

Non-null assertion and assertions on narrowed type:

expectThat(subject)
  .isNotNull()
  .and {
//  ...
  }

Use .isNotNull() to narrow nullability and .isA<Int>() to narrow to specific type.

Grouping

Use .and { } to group assertions on a subtree

Chained Assertions

These fail fast:

expectThat(subject)
  .isA<String>()
  .hasLength(1)

Block Assertions

Will run all assertions in the block before failing:

expectThat(subject) {
  isA<String>()
  hasLength(1)
}

or as a subblock

.and(subject) {
  isA<String>()
  hasLength(1)
}

Asserting Different Subjects

expect {
  that(varOne)
    .isA<String>()
    .hasLength(1)
  that(varTwo) {
    isLessThan(1L)
}

Sub-trees

Using get to assert sub-properties

expectThat(subject) {
  get { name }.isEqualTo("Ziggy")
  get { birthDate.year }.isEqualTo(1971)
}

or with property/method references:

expectThat(subject) {
  get(Person::name).isEqualTo("David")
  get(Person::birthDate).get(LocalDate::getYear).isEqualTo(1947)
}

or as separate assertions

expect {
  that(person.name).isEqualTo("David")
  that(person.birthDate.year).isEqualTo(1947)
}

Traversals and Modifers

Combine these with asserts to traverse to different objects or modify the object under test:

  • CharSequence .trim() : trims a string
  • File .toPath() : returns the file path string
  • File .lines() : returns contents of file as a collection of lines
  • File .text() : returns contents of file as text
  • Iterable .all() : traverses to collection members and applies assertions to all items
  • Iterable .map() : allows assertions to be mapped to each collection member
  • Iterable .first() : returns first element
  • Iterable .first { predicate } : returns first matching element
  • Iterable .single() : returns single element
  • Iterable .last() : returns last element
  • Iterable .flatMap() : Flattens nested collections
  • Iterable .filter { predicate } : Filters matching elements
  • Iterable .filterNot { predicate } : Filters non-matching elements
  • Iterable .filterIsInstance<R>() : Filters for matching instance types
  • Iterable/List/Map .get(n) : returns element n
  • Iterable/List .get(range) : returns elements in range
  • Map .get(key) : returns element for key
  • Map .getValue(key) : returns element for key (asserts for existence)
  • Optional .toNullable() : returns kotlin nullable type from Java Optional
  • Path .resolve(other) : returns the combined paths
  • Path .toFile() : returns file for path
  • Path .allBytes() : returns bytes of all files on path???
  • Path .allLines() : returns lines of all files on path???
  • Path .allLines(charset) : returns lines of all files on path converted using charset???

Collections

Mapping elements of exceptions:

expectThat(subject)
  .map(Person::name)
  .containsExactly("David", "Ziggy", "Aladdin", "Jareth")

Collection-specific traversals:

  • Array toList() : List : Traverses from an array to a list
  • Collection single() : T : Traverses to the single element of a single element collection

(See also traversals, above, for more options)

Collection-specific assertions:

  • Collection .contains(expectedElements) : That the collection contains all the expected elements
  • Collection .hasSize(expectedSize) : That the collection has the expected size
  • Collection .isSorted(comparator) : That the collection is in the order expected by the comparator
  • Collection .doesNotContain(unexpectedElements) : That the collection does not contain these elements
  • Collection .containsExactly(expectedElements) : That the collection contains all and only the expected elements
  • Collection .containsExactlyInAnyOrder(expectedElements) : That the collection contains exactly the expect elements, order independent
  • List .containsSequence(expected) : That the list contains the elements in the same order

Asserting contents

expectThat() with .all / .any / .none

Failures

Checking that a block throws an exception:

expectCatching { identifyHotdog("hamburger") }
  .failed()
  .isA<NotHotdogException>()

or

expect {
  catching { identifyHotdog("hamburger") }
    .failed()
    .isA<NotHotdogException>()
}

or

expectThrows<NotHotdogException> {
  identifyHotdog("hamburger")
}

Checking that a block does not throw an exception:

expectCatching { identifyHotdog("hotdog") }
  .succeeded()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment