Code Monkey home page Code Monkey logo

mockito4kotlin.annotation's Introduction

Mockito Annotations for Kotlin

Kotlin Mockito MIT License

Travis.Build Download

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).

Content

Installing

Mockito Annotations for Kotlin is available on jcenter.

gradle

testCompile 'org.mockito4kotlin:annotation:0.3.x'

maven

<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>

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-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
}

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-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
}

mockito4kotlin.annotation's People

Contributors

wickie73 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.