Mockito Annotations for Kotlin

Mockito Annotations for Kotlin: a small Kotlin library which supports Annotations for Mockito and Mockito-Kotlin.

License

License

MIT
Categories

Categories

Kotlin Languages Mockito Unit Testing
GroupId

GroupId

io.github.wickie73
ArtifactId

ArtifactId

mockito4kotlin-annotation
Last Version

Last Version

0.5.0
Release Date

Release Date

Type

Type

jar
Description

Description

Mockito Annotations for Kotlin
Mockito Annotations for Kotlin: a small Kotlin library which supports Annotations for Mockito and Mockito-Kotlin.
Project URL

Project URL

https://github.com/wickie73/mockito4kotlin-annotation
Source Code Management

Source Code Management

https://github.com/wickie73/mockito4kotlin-annotation/tree/main

Download mockito4kotlin-annotation

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.wickie73/mockito4kotlin-annotation/ -->
<dependency>
    <groupId>io.github.wickie73</groupId>
    <artifactId>mockito4kotlin-annotation</artifactId>
    <version>0.5.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.wickie73/mockito4kotlin-annotation/
implementation 'io.github.wickie73:mockito4kotlin-annotation:0.5.0'
// https://jarcasting.com/artifacts/io.github.wickie73/mockito4kotlin-annotation/
implementation ("io.github.wickie73:mockito4kotlin-annotation:0.5.0")
'io.github.wickie73:mockito4kotlin-annotation:jar:0.5.0'
<dependency org="io.github.wickie73" name="mockito4kotlin-annotation" rev="0.5.0">
  <artifact name="mockito4kotlin-annotation" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.wickie73', module='mockito4kotlin-annotation', version='0.5.0')
)
libraryDependencies += "io.github.wickie73" % "mockito4kotlin-annotation" % "0.5.0"
[io.github.wickie73/mockito4kotlin-annotation "0.5.0"]

Dependencies

compile (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.32
org.jetbrains.kotlin : kotlin-reflect jar 1.4.32
org.jetbrains.kotlinx : kotlinx-coroutines-core-jvm jar 1.4.3
org.mockito : mockito-core jar 3.8.0
org.mockito.kotlin : mockito-kotlin jar 3.1.0

runtime (1)

Group / Artifact Type Version
org.jetbrains.kotlinx : kotlinx-coroutines-jdk8 jar 1.4.3

test (3)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.7.1
org.assertj : assertj-core jar 3.18.1
org.junit.jupiter : junit-jupiter-engine jar 5.7.1

Project Modules

There are no modules declared in this project.

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
}

Versions

Version
0.5.0
0.4.6
0.4.5