mockito-kotlin icon indicating copy to clipboard operation
mockito-kotlin copied to clipboard

How to mock lambda call

Open cjacky475 opened this issue 2 years ago • 1 comments

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?

cjacky475 avatar Sep 27 '22 12:09 cjacky475

Are you asking about InvocationOnMock?

wesalvaro avatar Apr 28 '23 06:04 wesalvaro