Skip to content
tezvyn:
Menu

Isolating Units for Tests with Mockito-Kotlin

Source: github.comMediumHow cards are made

Mockito-Kotlin creates fake objects (mocks) to stand in for real dependencies in your tests. This lets you test a class in isolation, for example, testing a ViewModel without making a real network call. The footgun is testing implementation, not behavior.

Why it exists

When unit testing a piece of code, you want to test it in isolation. If your PaymentProcessor class depends on a NetworkClient and a Database, running a test for it would require a live network and database. This is slow, unreliable, and complex. Test doubles were invented to solve this by replacing real dependencies with fakes you can control.

The mental model

A mock is a stand-in actor. Imagine you're testing a Checkout class that uses a CreditCardService. Instead of using a real service and charging a real card, you create a mock CreditCardService. You program this mock with specific instructions: "When charge() is called with $10, return 'Success'". Then you run your Checkout logic and verify that it did, in fact, call charge() on the service as expected.

How it works

Mockito-Kotlin is a wrapper around the popular Java library Mockito, providing a more idiomatic Kotlin syntax. It uses functions like mock<Type>() to create a fake object at runtime. You then use a block like on { mock.someMethod() } doReturn "someValue" to "stub" its behavior, defining what it should do when called. Finally, after executing your code under test, you use verify(mock).someMethod() to assert that your code interacted with the dependency correctly.

When to use it

Use mocks for unit testing to isolate your class under test (the "SUT"). It's perfect for replacing dependencies that are slow (network APIs), have side effects (database writes), are non-deterministic (return the current time), or are simply complex to set up for a test.

When not to use it

Do not use mocks for integration tests, where the entire point is to see how real components interact. Avoid mocking simple value objects or data classes you can easily create. The biggest pitfall is over-mocking, where tests become a fragile mirror of the implementation. If you refactor the SUT's internal logic without changing its public behavior, a heavily mocked test might break, which is a bad sign.

One canonical example

Imagine testing a UserManager that depends on a UserApi to fetch data. First, in the "Given" block, you create a mock API: val mockApi = mock<UserApi>. You then program its behavior: on { mockApi.fetchUserName(1) } doReturn "Alice". This tells the mock to return "Alice" when its fetchUserName method is called with the ID 1. Next, in the "When" block, you call your method under test: userManager.getFormattedUserName(1). Finally, in the "Then" block, you verify the outcome. You assert that the result is correct, and you can also use verify(mockApi).fetchUserName(1) to confirm that your UserManager did in fact call the API as expected.

Interview question

Which scenario best illustrates the primary benefit of using Mockito-Kotlin in unit testing?

  • a.Verifying that a utility class's private helper methods are invoked correctly during its public method execution.
  • b.Accelerating the execution of integration tests by providing pre-defined responses for all external API calls.
  • c.Replacing a database dependency with a controlled fake to test a repository's data manipulation logic in isolation.Correct
  • d.Ensuring that a ViewModel's network calls are executed against a live server to confirm end-to-end connectivity.
Why?

Mockito-Kotlin's main purpose is to isolate the class under test by replacing slow, unreliable, or complex dependencies (like a database) with controlled fakes, enabling focused and reliable unit tests. Option D describes an integration test, which mocks are explicitly not for. Option A focuses on internal implementation details, which is a common pitfall.

Just read this? Test yourself on what you have been reading.

Read the original → github.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles