Skip to content

StepVerifier expectErrorMatches() extension #34

Open
@kiwisincebirth

Description

@kiwisincebirth

Motivation

I'm looking to ease development of testing code using Step Verifier and checking for Exception Types and messages to ensure the appropriate error was raised.

// This is how the existing test code looks
myService.myFunction().test()
    .verifyErrorMatches { it is MyException && it.message == "My Message" }

// And this is a more Kotlin idiomatic code could look like
myService.myFunction().test()
    .verifyErrorMatches<MyException> { it.message == "My Message" }

While the above is not dissimilar it does seperate out the typing from the predicate, which adds to clarity IMHO. Also note the it in the second example is typed to MyException so you can test properties with out further casting.

Desired solution

This is the functions which would implement this

inline fun <reified T : Throwable> LastStep.expectErrorMatches(crossinline predicate: (T)->Boolean = {true}): StepVerifier =
    expectErrorMatches { it is T && predicate(it) }

inline fun <reified T : Throwable> LastStep.verifyErrorMatches(crossinline predicate: (T)->Boolean = {true}): Duration = 
    verifyErrorMatches { it is T && predicate(it) }

Considered alternatives

Haven't consider other alternatives, except verifyErrorMessage() expectErrorMessage() but these dont allow checking of type. That I am aware, so dont offer the full ability of xxxErrorMatches()

Metadata

Metadata

Assignees

No one assigned

    Labels

    ❓need-triageThis issue needs triage, hasn't been looked at by a team member yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions