question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Verify Extension Function / kotlin

See original GitHub issue
@RunWith(MockitoJUnitRunner::class)
internal class HighOrderTest {
    @Mock
    lateinit var a: A

    @Test
    fun test() {
        // When
        a.run()
        
       // Then
        verify(a).doSomething()
    }

}

open class A

fun A.run() {
    doSomething()
}

fun A.doSomething() {

}

The above test will fail with:

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:

I guess it’s not possible yet to verify the high order doSomething. Is it possible to implement this feature? If not a explicit error would be helpful I think

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
wengelefcommented, Apr 20, 2018

those are extension functions dudes, not high-order functions.

0reactions
wengelefcommented, Apr 24, 2018

anyway, this works for me.

class A

fun A.foo(): String {
    return bar()
}

fun A.bar(): String {
    return "test"
}

init {
    val a = mock<A>()

    "when extension A.foo Function is called, it should return '\"test\"" {
        a.foo() shouldBe "test"
        verify(a, times(1)).foo()
        verify(a, times(1)).bar()
    }
}

are you using mockito-kotlin?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How should I test Kotlin extension functions? - Stack Overflow
1 Answer 1 ... Well, to test a method, whether static or not, you call it as real code would do, and you...
Read more >
Testing Kotlin extension functions | www.rolfje.com
This test will not only run your extension function, but you can even verify that the extension function calls the correct functions on...
Read more >
Mockk- Testing Extension Functions | by Byte Sized Bits
Extension functions can be useful to extend an existing library easily. Testing can be sometimes a bit challenging.
Read more >
Extensions | Kotlin
This mechanism is called an extension function. There are also extension properties that let you define new properties for existing classes.
Read more >
Testing Kotlin extension functions | Rolfje's blog
This test will not only run your extension function, but you can even verify that the extension function calls the correct functions on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found