Skip to content

wickie73/mockito4kotlin-annotation

Repository files navigation

Mockito Annotations for Kotlin

Kotlin Mockito MIT License

Travis.Build Download

This is a small Kotlin library which supports Annotations for Mockito 2.x or Kotlin libraries based on Mockito like mockito-kotlin.

In this library the initialization of fields annotated with Mockito annotations by code MockitoAnnotations.initMocks(testClass) is replaced by KMockitoAnnotations.initMocks(testClass) which is written in Kotlin and supports most of Kotlin specific features. It is compatible with MockitoAnnotations.initMocks(testClass).

Content

Installing

Mockito Annotations for Kotlin is available on jcenter.

gradle

testCompile 'io.github.wickie73:mockito4kotlin-annotation:0.5.x'

maven

<dependency>
    <groupId>io.github.wickie73</groupId>
    <artifactId>mockito4kotlin-annotation</artifactId>
    <version>0.5.x</version>
    <scope>test</scope>
</dependency>

Examples

Mock with Annotation:

@Mock
lateinit var addressDAO: AddressDAO
@Mock
lateinit var address: Address

@Before
fun setUp() {
    KMockitoAnnotations.initMocks(this)
}

@Test
fun testService() {
    val addressList = listOf(address)

    // with Mockito
    `when`(addressDAO.getAddressList()).thenReturn(addressList)

    // or with Mockito-Kotlin
    whenever(addressDAO.getAddressList()).thenReturn(addressList)
}

Spy with Annotation:

@Spy
var addressDAO = AddressDAO()
@Mock
lateinit var address: Address

@Before
fun setUp() {
    KMockitoAnnotations.initMocks(this)
}

@Test
fun testService() {
    val addressList = listOf(address)

    // with Mockito
    `when`(addressDAO.getAddressList()).thenReturn(addressList)

    // or with Mockito-Kotlin
    whenever(addressDAO.getAddressList()).thenReturn(addressList)
}

ArgumentCaptor with Annotation:

@Captor
lateinit var captor: ArgumentCaptor<Address>
@Mock
lateinit var addressDAO: AddressDAO

@Before
fun setUp() {
    KMockitoAnnotations.initMocks(this)
}

@Test
fun testService() {
    val address: Address().apply {
        street = "Abbey Road 73"
        city = "London"
    }

    addressDAO.save(address)

    verify(addressDAO).save(captor.capture())
    assertEquals(address, captor.value)
}

interface AddressDAO {
    fun getAddressList(): List<Address>
    fun save(address: Address?)  // 'Address?' has to be nullable here
}

Mockito-Kotlin KArgumentCaptor with KCapture Annotation:

@KCaptor
lateinit var captor: KArgumentCaptor<Address>
@Mock
lateinit var addressDAO: AddressDAO

@Before
fun setUp() {
    KMockitoAnnotations.initMocks(this)
}

@Test
fun testService() {
    val address: Address().apply {
        street = "Abbey Road 73"
        city = "London"
    }

    addressDAO.save(address)

    verify(addressDAO).save(captor.capture())
    assertEquals(address, captor.firstValue)
}

interface AddressDAO {
    fun getAddressList(): List<Address>
    fun save(address: Address)  // 'Address' has not to be nullable here
}

Inject Mocks with Annotation:

@Spy
lateinit var addressList: List<Address>
@Mock
lateinit var addressDatabase: AddressDatabase
@InjectMocks
val addressDAO: AddressDAOImpl()

@Before
fun setUp() {
    KMockitoAnnotations.initMocks(this)
}

@Test
fun testService() {
    // with Mockito
    `when`(addressList.size()).thenReturn(2)

    // or with Mockito-Kotlin
    whenever(addressList.size()).thenReturn(2)

    verify(addressDatabase).addListener(any(ArticleListener.class))

    assertEquals(addressList, addressDAO.addressList)
    assertEquals(addressDatabase, addressDAO.addressDatabase)
    assertThat(addressDAO.addressList).hasSize(2)
}

class AddressDAOImpl {
    lateinit var addressList: List<Address>
    lateinit var addressDatabase: AddressDatabase
}

Limitations

Stubbing does not work with

  • immutable properties ( val address: Address() )
  • properties of final classes (use interface or open class )
  • properties of sealed classes (only @Spy )
  • properties of private/internal inner classes
  • properties of companion objects
  • properties of objects
  • delegated properties ( var p: String by Delegate() )

Instead stubbing works with

  • properties in sealed classes
  • properties in private/internal inner classes
  • properties in companion objects
  • properties in objects
  • properties in data classes
  • properties of data classes

@KCapture vs. @Captor Annotation

Mockitos ArgumentCaptor#capture() returns null. So like in this example the type of the argument of method save(address: Address?) in interface AddressDAO has to be nullable:

@Captor
lateinit var captor: ArgumentCaptor<Address>
// ...
KMockitoAnnotations.initMocks(this)
// ...
verify(addressDAO).save(captor.capture())
// with:
interface AddressDAO {
    fun save(address: Address?)  // 'Address?' has to be nullable here
}

With Mockito-Kotlin KArgumentCaptor you don't have to be care about nullable parameters:

@KCaptor
lateinit var captor: KArgumentCaptor<Address>
// ...
KMockitoAnnotations.initMocks(this)
// ...
verify(addressDAO).save(captor.capture())
// with:
interface AddressDAO {
    fun save(address: Address)  // 'Address' has not to be nullable here
}