This is a small Kotlin library which supports Annotations for Mockito or Kotlin libraries based on Mockito like Mockito-Kotlin2 or Mockito4k.
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).
Mockito Annotations for Kotlin is available on jcenter.
testCompile 'org.mockito4kotlin:annotation:0.3.x'
<dependency>
<groupId>org.mockito4kotlin</groupId>
<artifactId>annotation</artifactId>
<version>0.3.x</version>
<scope>test</scope>
</dependency>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-wickie73-maven</id>
<name>bintray</name>
<url>https://bintray.com/wickie73/wickieMaven</url>
</repository>
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-Kotlins 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
}
Stubbing does not work with
- immutable properties (
val address: Address()
) - properties of final classes (use
interface
oropen 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
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-Kotlin2s 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
}