0
0
Android Kotlinmobile~3 mins

Why Repository testing with fakes in Android Kotlin? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your tests could run instantly and never fail because of slow or missing data?

The Scenario

Imagine you want to test your app's data fetching, but it depends on a real database or a web service that is slow or sometimes unavailable.

You try to run tests, but they fail or take forever because they rely on these real parts.

The Problem

Running tests with real databases or network calls is slow and flaky.

It can cause tests to fail randomly due to network issues or data changes.

This makes it hard to trust your tests and slows down your development.

The Solution

Using fakes means creating simple, pretend versions of your data sources.

These fakes behave like the real ones but are fast and reliable.

This lets you test your repository logic quickly and safely without real dependencies.

Before vs After
Before
val data = realDatabase.getData()
assert(data.isNotEmpty())
After
val fakeDb = FakeDatabase()
val data = fakeDb.getData()
assert(data.isNotEmpty())
What It Enables

It enables fast, reliable tests that focus on your app's logic, not on external systems.

Real Life Example

When building a weather app, you can fake the weather API to return fixed data, so your tests never fail due to network problems.

Key Takeaways

Manual tests with real data sources are slow and unreliable.

Fakes simulate data sources for fast and stable tests.

This improves confidence and speed in app development.