Skip to content

Using pattern matchers in tests #796

@filiphornak

Description

@filiphornak

Context

In PR #791 (comment) we discussed with @jozefbakus an option to use pattern matching in scala test. This will reduce the boilerplate for testing that Option, Try, Either, or any other sum type has a good result.

Here is an actual example taken from ChekpointServiceTest

result.size shouldBe 2
result.head._1 shouldBe "my.topic"
result.head._2 should contain theSameElementsAs Map(2 -> 2021L, 1 -> 1021L, 3 -> 3021L, 0 -> 21L)
result.toSeq(1)._1 shouldBe "my.other.topic"
result.toSeq(1)._2 should contain theSameElementsAs Map(0 -> 0L)

which could be reduced to

result shouldBe Seq(
  ("my.topic", Map(0 -> 21L, 1 -> 1021L, 2 -> 2021L, 3 -> 3021L)),
  ("my.other.topic", Map(0 -> 0L))
)

or if we don't care about exact values, we can use `should matchPatter { case => }

val result = underTest.getLatestCommittedOffset(params)(ugi)

result.get.isDefined shouldBe true
result.get shouldBe Some(offsets)

which will become

result should matchPattern { case Success(Some(_)) => }

Objective

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions