mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.09k stars 198 forks source link

How to mock lambda call #463

Open cjacky475 opened 1 year ago

cjacky475 commented 1 year ago

Hello, I have this function:

override fun loadSensors(userId: String, param: (Result<MutableList<UserSensor>>) -> Unit) {
        // Blank
}
    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @Rule @JvmField
    var mockitoRule = MockitoJUnit.rule()

    @Inject
    lateinit var repository: MockRepositoryImpl

    @Before
    fun before() {
        hiltRule.inject()

        MockitoAnnotations.openMocks(this)

        `when`(repository.loadSensors(anyString(), any())).thenAnswer {
            Result.Success("", mutableListOf(UserSensor(0), UserSensor(1)))
        }

        ActivityScenario.launch(MainActivity::class.java)
    }

How to mock that thenAnswer would actually call my param argument?

wesalvaro commented 1 year ago

Are you asking about InvocationOnMock?