
Of course, we can add the open keyword to classes and methods that we want to mock. Most JVM mock libraries have problems with mocking or stubbing final classes. It exists and it is called function type.

While this helps us write immutable code, it also causes some problems during testing. While Kotlin is statically typed, to make it possible, functions need to have a type. Now let’s understand how “ just runs” skips the real function call. In Kotlin, all classes and methods are final. If we skip the function’s execution successfully, “Hello World!” shouldn’t be added to the given list after the invocation. And then, we call the function and pass an empty MutableList to it. This example demonstrates how to declare various. Let’s create a service that requires two instances of our TestableService: class InjectTestService just runsĪs the test above shows, we stub the addHelloWorld() function using “ just runs“. You can mix any type of repository described in the reference section.

It is possible to use MockK annotations to create all kind of mocks.
