Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock.inOrder {} poor design prevent it to work #472

Open
mitasov-ra opened this issue Jan 4, 2023 · 0 comments
Open

mock.inOrder {} poor design prevent it to work #472

mitasov-ra opened this issue Jan 4, 2023 · 0 comments

Comments

@mitasov-ra
Copy link

mitasov-ra commented Jan 4, 2023

Verification.kt contains function:

/**
 * Allows [InOrder] verification for a single mocked instance:
 *
 * mock.inOrder {
 *    verify().foo()
 * }
 *
 */
inline fun <T> T.inOrder(block: InOrderOnType<T>.() -> Any) {
    block.invoke(InOrderOnType(this))
}

Which looks really handy but doesn't work if mock is created with default mock behaviour (all methods return null).

That's because of block: InOrderOnType<T>.() -> Any part, which says that block must return something non-null.
Kotlin always adds runtime null-checks at call sites, hence the example code

mock.inOrder {
   verify().foo()
}

will always fail with NPE unless mock is configured with when to return something non-null when .foo() is called.

To fix this, the signature of inOrder should be either

inline fun <T> T.inOrder(block: InOrderOnType<T>.() -> Any?)

or

inline fun <T> T.inOrder(block: InOrderOnType<T>.() -> Unit)

Until then the only way of using it is:

mock.inOrder {
   verify().foo()
   "hey, inOrder creator messed up, so this line is required to make it work"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant