# Unit Tests with Mockk always fails on CI but works on locally

**URL:** <https://discuss.bitrise.io/t/unit-tests-with-mockk-always-fails-on-ci-but-works-on-locally/24943>\
**Category:** Android\
**Tags:** android\
**Created:** [February 12, 2025, 12:50pm UTC](https://discuss.bitrise.io/t/unit-tests-with-mockk-always-fails-on-ci-but-works-on-locally/24943 "2025-02-12T12:50:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sevbanBayir](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@sevbanBayir](https://discuss.bitrise.io/u/sevbanBayir)\
**Post date:** [February 12, 2025, 12:50pm UTC](https://discuss.bitrise.io/t/unit-tests-with-mockk-always-fails-on-ci-but-works-on-locally/24943/1 "2025-02-12T12:50:11Z")

</div>

Hi I have some unit tests for my multi-modular Android Project and I want to run a CI pipeline with these tests but as the header says some tests that are using mockk to mocking operations are failing on CI server (using Bitrise for this with a Ubuntu 22.04 machine) but every time in any situation works on my local machine (MB M1 Pro). I did a lot of research but neither any LLM out there or any blog couldn’t help.

Here is my test class in my `:feature_home` module:

```kotlin
@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(MainCoroutineExtension::class)
class DetailViewModelTest {
    private lateinit var viewModel: DetailViewModel
    private lateinit var getForecastUseCase: GetForecastUseCase
    private lateinit var savedStateHandle: SavedStateHandle

    @BeforeEach
    fun setUp() {
        getForecastUseCase = mockk(relaxed = true)
        savedStateHandle = SavedStateHandle()
        viewModel = DetailViewModel(getForecastUseCase, savedStateHandle)
    }

    @Test
    fun `given viewModel when initialized then forecastState should be Loading`() = runTest {
        assertThat(viewModel.forecastState.value).isEqualTo(ForecastState.Loading)
    }

    @Test
    fun `given savedStateHandle with null values when fetching forecast then state should remain Loading`() =
        runTest {
            savedStateHandle[DetailViewModel.LATITUDE_ARG] = null
            savedStateHandle[DetailViewModel.LONGITUDE_ARG] = null

            every { getForecastUseCase.execute(any(), any()) } returns flowOf(dummyForecast)

            viewModel.forecastState.test {
                val firstItem = awaitItem()
                assertThat(firstItem).isEqualTo(ForecastState.Loading)
                expectNoEvents()
            }
        }
}

```

(on CI)  
Here, first test always succeeds but the second one (which contains mocking operations) always fails with exception :

> java.lang.NullPointerException: Cannot invoke  
> “com.sevban.data.repository.WeatherForecastRepository.getLocationWeather(String,  
> String)” because “this.repository” is null …

As you can see its complaining about this.repository being null but the thing is I’m not mocking any repository, I’m only mocking the `GetForecastUseCase` class that uses a repository as a dependency for making requests to API.

**What did I try ?**

- I ran repeated tests thousands of times to see whether they are flakky tests or not but they never failed in my local machine.

- Also I cleaned my gradle & Android Studio cache a lot of times but they were still succeeding in local, failing online.

Are there any suggestions to try on or should this be related with an issue about Bitrise ? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![silvercast.nguyen](https://avatars.discourse-cdn.com/v4/letter/s/dbc845/32.png) [@silvercast.nguyen](https://discuss.bitrise.io/u/silvercast.nguyen)\
**Post date:** [February 13, 2025, 11:54am UTC](https://discuss.bitrise.io/t/unit-tests-with-mockk-always-fails-on-ci-but-works-on-locally/24943/2 "2025-02-13T11:54:13Z")

</div>

Hi @sevbanBayir , would you mind open a support ticket so we can take a deeper look at your issue?  
Best regards
