Static method imports in Kotlin

Kotlin

Kotlin Problem Overview


How can a method be statically imported in Kotlin? For example, in Java it's possible to do:

...
import static org.mockito.Mockito.verify;
...
class FoobarTest {
     ...
     @Test public void testFoo() {
          verify(mock).doSomething();
     } 
}

How can the same be done in Kotlin without having to fully qualify the method every time with Mockito.verify(mock).doSomething()?

Kotlin Solutions


Solution 1 - Kotlin

It turns out it's very easy. To import a single static method:

import org.mockito.Mockito.verify

And to import everything:

import org.mockito.Mockito.*

so it will be possible to do

`when`(someMock.someAction).thenReturn(someResult)
verify(mock).doSomething()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmemoizrView Question on Stackoverflow
Solution 1 - KotlinmemoizrView Answer on Stackoverflow