fbpx

Mocking and stubbing suspend functions with MockK

MockK over the years became my go-to library for mocking suspend functions. Its intuitive syntax and built-in coroutine support simplify testing asynchronous operations. With MockK, you can easily replicate complex behaviors of coroutine-based functions, ensuring tests are reflective of real-world scenarios.

Mock – fake version of an object used in testing to check how other parts of a program interact with it.

Stub – fake part of the program that always returns the same result.

This article will show you how to use both in testing Kotlin code with MockK.

Testing time-based code with Joda Time

In some systems you sometimes need to record date or timestamp of given action. Good example could be comments system, where each comment has timestamp: In our experiment we will perform two test cases: So how can we manipulate time in unit test? Inject time provider to system under test That’s probably the most desirable […]

Parameterized tests with Kotest

Creating parameterized tests in Kotlin: data driven testing, collection inspections and generating test cases with FreeSpec.

Handling exceptions in tests: Junit & Kotest

Exceptions and throwables are essential part of many Java APIs. They are useful for modeling application domain layer and controlling program execution flow. We’ll be working on following piece of code: Annotated Junit4 method Not perfect – the whole test method listens for `CustomException` to be thrown anywhere in method – and with junit4 tools […]

Assert softly – when one assertion is not enough

It’s usually in our best interest to keep one assertion per test method. Yet, situation when more than one check in single test method is present may occur. Why this may be a problem?Generally, JVM testing frameworks, such as Junit makes use of AssertionExceptions. If such exception is thrown during test block execution, test stops and […]