Using MockK library in Jetpack Compose Preview

Starting

What is Preview? It’s a functionality that gives your an opportunity for view a composable UI during development without the need to build and run on an emulator. (If you use the Android Studio obviously)

Problem

In fact…


This content originally appeared on DEV Community and was authored by Aleksei Laptev

Starting

What is Preview? It's a functionality that gives your an opportunity for view a composable UI during development without the need to build and run on an emulator. (If you use the Android Studio obviously)

Problem

In fact you must have a some data that you wanna see in preview. But sometimes it's impossible.

Imagine, you have a screen like this:

@Composable
fun SomeScreen(
    viewModel: SomeViewModel = hiltViewModel(),
){
    //some UI
}

And view model of that screen like this:

class SomeViewModel @Inject constructor(
    private val someUseCase1: someUseCase1,
    private val someUseCase2: someUseCase2
    //etc
) : ViewModel() {

    val someDataFLow1 = someUseCase1.getSomeData()
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000),
            initialValue = null
        )

    //etc
}

And that's all. How can you make a preview? - Directly, no how.

Solution

Use MockK library. It's an open-source and excellent library for testing. Okay, it's for testing... But why can't we use it for debugging and preview? - Of course, we can!

First of all we have to add dependencies

//Compose
implementation(platform("androidx.compose:compose-bom:<bom-version>"))
//other compose dependencies

//Preview
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-tooling-preview")
//MockK
debugImplementation("io.mockk:mockk:<mockk-version>")

Here's our project structure

└── src/
    ├── main/
    │   └── kotlin/
    │       └── yourpackage/
    │           ├── SomeScreen.kt      
    │           └── SomeViewModel.kt    
    │
    └── debug/
        └── kotlin/
            └── yourpackage/
                └── preview/
                      ├── SomeScreenPreview.kt
                      └── MockEntities.kt

We have to add a mock ViewModel by MockK library and setup it's behavior:

internal object MockEntities {
    private val mockData = listOf(
        SomeDataItem(id = 1),
        SomeDataItem(id = 2),
        SomeDataItem(id = 3),
        SomeDataItem(id = 4),
    )

    private val mockDataFlow1 = MutableStateFlow(mockData)

    val someViewModel = mockk<SomeViewModel>().apply {
        every { someDataFLow1 } returns mockDataFlow1
    }
}

And a final function is a Preview itself:

@Composable
@Preview
private fun SomeScreenPreview() {
    YourTheme {
        SomeScreen(
            viewModel = MockEntities.someViewModel
        )
    }
}

So, that was a little trick. I hope it helps you in your work :)


This content originally appeared on DEV Community and was authored by Aleksei Laptev


Print Share Comment Cite Upload Translate Updates
APA

Aleksei Laptev | Sciencx (2025-11-01T20:16:55+00:00) Using MockK library in Jetpack Compose Preview. Retrieved from https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/

MLA
" » Using MockK library in Jetpack Compose Preview." Aleksei Laptev | Sciencx - Saturday November 1, 2025, https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/
HARVARD
Aleksei Laptev | Sciencx Saturday November 1, 2025 » Using MockK library in Jetpack Compose Preview., viewed ,<https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/>
VANCOUVER
Aleksei Laptev | Sciencx - » Using MockK library in Jetpack Compose Preview. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/
CHICAGO
" » Using MockK library in Jetpack Compose Preview." Aleksei Laptev | Sciencx - Accessed . https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/
IEEE
" » Using MockK library in Jetpack Compose Preview." Aleksei Laptev | Sciencx [Online]. Available: https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/. [Accessed: ]
rf:citation
» Using MockK library in Jetpack Compose Preview | Aleksei Laptev | Sciencx | https://www.scien.cx/2025/11/01/using-mockk-library-in-jetpack-compose-preview/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.