{"id":37027714,"url":"https://github.com/wickie73/mockito4kotlin-annotation","last_synced_at":"2026-01-14T03:18:00.231Z","repository":{"id":52840384,"uuid":"122778419","full_name":"wickie73/mockito4kotlin-annotation","owner":"wickie73","description":"Mockito Annotations for Kotlin","archived":false,"fork":false,"pushed_at":"2022-04-24T20:19:56.000Z","size":638,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-07-28T10:11:27.914Z","etag":null,"topics":["annotations","kotlin","kotlin-testing","mockito","mockito-kotlin","test-driven-development","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wickie73.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-24T20:47:21.000Z","updated_at":"2021-04-20T22:01:12.000Z","dependencies_parsed_at":"2022-08-20T02:01:06.532Z","dependency_job_id":null,"html_url":"https://github.com/wickie73/mockito4kotlin-annotation","commit_stats":null,"previous_names":[],"tags_count":11,"template":null,"template_full_name":null,"purl":"pkg:github/wickie73/mockito4kotlin-annotation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wickie73%2Fmockito4kotlin-annotation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wickie73%2Fmockito4kotlin-annotation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wickie73%2Fmockito4kotlin-annotation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wickie73%2Fmockito4kotlin-annotation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wickie73","download_url":"https://codeload.github.com/wickie73/mockito4kotlin-annotation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wickie73%2Fmockito4kotlin-annotation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408824,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotations","kotlin","kotlin-testing","mockito","mockito-kotlin","test-driven-development","unit-testing"],"created_at":"2026-01-14T03:17:59.574Z","updated_at":"2026-01-14T03:18:00.226Z","avatar_url":"https://github.com/wickie73.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mockito Annotations for Kotlin\n\n![Kotlin](https://img.shields.io/badge/Kotlin-1.4%2B-blue.svg?longCache=true)\n![Mockito](https://img.shields.io/badge/Mockito-3.8.0%2B-blue.svg?longCache=true)\n[![MIT License](http://img.shields.io/badge/license-MIT-green.svg?longCache=true)](https://github.com/wickie73/mockito4kotlin-annotation/blob/master/LICENSE)\n\n![Travis.Build](https://travis-ci.org/wickie73/mockito4kotlin-annotation.svg?longCache=true)\n[ ![Download](https://maven-badges.herokuapp.com/maven-central/io.github.wickie73/mockito4kotlin-annotation/badge.svg) ](https://maven-badges.herokuapp.com/maven-central/io.github.wickie73/mockito4kotlin-annotation)\n\nThis is a small Kotlin library which supports Annotations for Mockito 2.x or Kotlin libraries based on Mockito like\n[mockito-kotlin](https://github.com/mockito/mockito-kotlin/tree/main/).\n\nIn this library the initialization of fields annotated with Mockito annotations by code\n`MockitoAnnotations.initMocks(testClass)`\n is replaced by\n`KMockitoAnnotations.initMocks(testClass)`\nwhich is written in Kotlin and supports most of Kotlin specific features.\nIt is compatible with [MockitoAnnotations.initMocks(testClass)](https://javadoc.io/doc/org.mockito/mockito-core/3.8.0/org/mockito/ArgumentMatcher.html).\n\n## Content\n* [Installing](#installing)\n* [Examples](#examples)\n* [Limitations](#limitations)\n* [@KCapture vs. @Captor Annotation](#kcapture-vs-captor-annotation)\n\nInstalling\n----------\n\nMockito Annotations for Kotlin is available on jcenter.\n### gradle\n```gradle\ntestCompile 'io.github.wickie73:mockito4kotlin-annotation:0.5.x'\n```\n### maven\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.wickie73\u003c/groupId\u003e\n    \u003cartifactId\u003emockito4kotlin-annotation\u003c/artifactId\u003e\n    \u003cversion\u003e0.5.x\u003c/version\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n\n```\nExamples\n--------\n\nMock with Annotation:\n```kotlin\n@Mock\nlateinit var addressDAO: AddressDAO\n@Mock\nlateinit var address: Address\n\n@Before\nfun setUp() {\n    KMockitoAnnotations.initMocks(this)\n}\n\n@Test\nfun testService() {\n    val addressList = listOf(address)\n\n    // with Mockito\n    `when`(addressDAO.getAddressList()).thenReturn(addressList)\n\n    // or with Mockito-Kotlin\n    whenever(addressDAO.getAddressList()).thenReturn(addressList)\n}\n```\n\nSpy with Annotation:\n\n```kotlin\n@Spy\nvar addressDAO = AddressDAO()\n@Mock\nlateinit var address: Address\n\n@Before\nfun setUp() {\n    KMockitoAnnotations.initMocks(this)\n}\n\n@Test\nfun testService() {\n    val addressList = listOf(address)\n\n    // with Mockito\n    `when`(addressDAO.getAddressList()).thenReturn(addressList)\n\n    // or with Mockito-Kotlin\n    whenever(addressDAO.getAddressList()).thenReturn(addressList)\n}\n```\n\nArgumentCaptor with Annotation:\n\n```kotlin\n@Captor\nlateinit var captor: ArgumentCaptor\u003cAddress\u003e\n@Mock\nlateinit var addressDAO: AddressDAO\n\n@Before\nfun setUp() {\n    KMockitoAnnotations.initMocks(this)\n}\n\n@Test\nfun testService() {\n    val address: Address().apply {\n        street = \"Abbey Road 73\"\n        city = \"London\"\n    }\n\n    addressDAO.save(address)\n\n    verify(addressDAO).save(captor.capture())\n    assertEquals(address, captor.value)\n}\n\ninterface AddressDAO {\n    fun getAddressList(): List\u003cAddress\u003e\n    fun save(address: Address?)  // 'Address?' has to be nullable here\n}\n```\n\n[Mockito-Kotlin](https://github.com/mockito/mockito-kotlin/tree/main/) KArgumentCaptor with KCapture Annotation:\n\n```kotlin\n@KCaptor\nlateinit var captor: KArgumentCaptor\u003cAddress\u003e\n@Mock\nlateinit var addressDAO: AddressDAO\n\n@Before\nfun setUp() {\n    KMockitoAnnotations.initMocks(this)\n}\n\n@Test\nfun testService() {\n    val address: Address().apply {\n        street = \"Abbey Road 73\"\n        city = \"London\"\n    }\n\n    addressDAO.save(address)\n\n    verify(addressDAO).save(captor.capture())\n    assertEquals(address, captor.firstValue)\n}\n\ninterface AddressDAO {\n    fun getAddressList(): List\u003cAddress\u003e\n    fun save(address: Address)  // 'Address' has not to be nullable here\n}\n```\n\nInject Mocks with Annotation:\n\n```kotlin\n@Spy\nlateinit var addressList: List\u003cAddress\u003e\n@Mock\nlateinit var addressDatabase: AddressDatabase\n@InjectMocks\nval addressDAO: AddressDAOImpl()\n\n@Before\nfun setUp() {\n    KMockitoAnnotations.initMocks(this)\n}\n\n@Test\nfun testService() {\n    // with Mockito\n    `when`(addressList.size()).thenReturn(2)\n\n    // or with Mockito-Kotlin\n    whenever(addressList.size()).thenReturn(2)\n\n    verify(addressDatabase).addListener(any(ArticleListener.class))\n\n    assertEquals(addressList, addressDAO.addressList)\n    assertEquals(addressDatabase, addressDAO.addressDatabase)\n    assertThat(addressDAO.addressList).hasSize(2)\n}\n\nclass AddressDAOImpl {\n    lateinit var addressList: List\u003cAddress\u003e\n    lateinit var addressDatabase: AddressDatabase\n}\n```\n\nLimitations\n-----------\n\nStubbing does not work with\n\n* immutable properties ( `val address: Address()` )\n* properties of final classes (use `interface` or `open class` )\n* properties of sealed classes (only `@Spy` )\n* properties of private/internal inner classes\n* properties of companion objects\n* properties of objects\n* delegated properties ( `var p: String by Delegate()` )\n\nInstead stubbing works with\n\n* properties in sealed classes\n* properties in private/internal inner classes\n* properties in companion objects\n* properties in objects\n* properties in data classes\n* properties of data classes\n\n@KCapture vs. @Captor Annotation\n--------------------------------\n\nMockitos `ArgumentCaptor#capture()` returns null. So like in this example\nthe type of the argument of method `save(address: Address?)` in interface `AddressDAO` has to be `nullable`:\n```kotlin\n@Captor\nlateinit var captor: ArgumentCaptor\u003cAddress\u003e\n// ...\nKMockitoAnnotations.initMocks(this)\n// ...\nverify(addressDAO).save(captor.capture())\n// with:\ninterface AddressDAO {\n    fun save(address: Address?)  // 'Address?' has to be nullable here\n}\n```\nWith [Mockito-Kotlin](https://github.com/mockito/mockito-kotlin) KArgumentCaptor you don't have to be care about\n`nullable` parameters:\n```kotlin\n@KCaptor\nlateinit var captor: KArgumentCaptor\u003cAddress\u003e\n// ...\nKMockitoAnnotations.initMocks(this)\n// ...\nverify(addressDAO).save(captor.capture())\n// with:\ninterface AddressDAO {\n    fun save(address: Address)  // 'Address' has not to be nullable here\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwickie73%2Fmockito4kotlin-annotation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwickie73%2Fmockito4kotlin-annotation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwickie73%2Fmockito4kotlin-annotation/lists"}