<commit_before>![mockk](doc/logo-s-alt2.png) ![kotlin](doc/kotlin-logo.png)

[![Gitter](https://badges.gitter.im/mockk-io/Lobby.svg)](https://gitter.im/mockk-io/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge) [![Build Status](https://travis-ci.org/oleksiyp/mockk.svg?branch=master)](https://travis-ci.org/oleksiyp/mockk) [![Relase Version](https://img.shields.io/maven-central/v/io.mockk/mockk.svg?label=release)](http://search.maven.org/#search%7Cga%7C1%7Cmockk)  [![Change log](https://img.shields.io/badge/change%20log-%E2%96%A4-yellow.svg)](https://github.com/oleksiyp/mockk/releases) [![Back log](https://img.shields.io/badge/back%20log-%E2%96%A4-orange.svg)](/BACKLOG) [![codecov](https://codecov.io/gh/oleksiyp/mockk/branch/master/graph/badge.svg)](https://codecov.io/gh/oleksiyp/mockk) [![Documentation](https://img.shields.io/badge/documentation-%E2%86%93-yellowgreen.svg)](#nice-features)

Table of contents:

* auto-gen TOC:
{:toc}

## Nice features

 - removing finals (via runtime class modification)
 - pure kotlin mocking DSL
 - partial arguments matchers
 - chained calls / deep stubs
 - matcher expressions
 - mocking coroutines
 - capturing lambdas
 - bunch of matchers
 - few verification modes

## Examples

 - [kotlin-fullstack-sample](https://github.com/Kotlin/kotlin-fullstack-sample/pull/28/files#diff-eade18fbfd0abfb6338dbfa647b3215dR17) project covered with tests

## Installation

There is three installation steps.

#### 1. Add dependency

<table>
<thead><tr><th>Tool</th><th>Instruction</th></tr></thead>
<tr>
<td width="100"><img src="doc/gradle.png" alt="Gradle"/></td>
<td>
    <pre>testCompile "io.mockk:mockk:1.4.1"</pre>
    </td>
</tr>
<tr>
<td><img src="doc/maven.png" alt="Maven"/></td>
<td>
<pre>&lt;dependency&gt;
    &lt;groupId&gt;io.mockk&lt;/groupId&gt;
    &lt;artifactId&gt;mockk&lt;/artifactId&gt;
    &lt;version&gt;1.4.1&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;</pre>
    </td>
</tr>
</table>

#### 2. Add class modification via annotation

Known issue: JaCoCo works only with agent

<table>
<thead><tr><th>Tool</th><th>Instruction</th></tr></thead>
<tr>
<td width="100"><img src="doc/junit4.png" alt="JUnit4"/></td>
<td>
Use annotation for each test:

<code>@RunWith(MockKJUnit4Runner::class) </code>

Use @ChainedRunWith or @RunWith on superclass to override delegated runner.

If neither is specified the default JUnit runner is used.
</td>
</tr><tr>
<td><img src="doc/junit5.png" alt="JUnit5"/></td>
<td>

JUnit5 tests should work auto-magically.

Note: this implementation is totally a hack.

To disable class modification just create empty file resource 'io/mockk/junit/mockk-classloading-disabled.txt' on a classpath.

</td>
</tr><tr>
<td width="100"><img src="doc/testng.png" alt="TestNG"/></td>
<td>
Add to your test class:

<pre>
class MockKClassTransformer {
  @ObjectFactory
  fun getObjectFactory(): IObjectFactory {
    return MockKObjectFactory()
  }
}
</pre>
</td>
</tr>
</table>

#### 3. Add class modification via agent (optional)

<table>
<thead><tr><th>Tool</th><th>Instruction</th></tr></thead>
<tr>
<td width="100"><img src="doc/gradle.png" alt="Gradle"/></td>
<td>
Add <a href="https://github.com/Zoltu/application-agent-gradle-plugin">agent</a> gradle plugin.

Use following agent:

<code>agent "io.mockk:mockk-agent:1.4.1"</code>

</td>
</tr><tr>
<td><img src="doc/maven.png" alt="Maven"/></td>
<td>
Add <code>dependency:properties</code> plugin.

Configure maven surefire plugin:

<code>&lt;argLine&gt;-javaagent:${io.mockk:mockk-agent:jar}&lt;/argLine&gt;</code>

See example <a href="https://github.com/oleksiyp/mockk/blob/master/example/sum/pom.xml">here</a>
</td>
</tr><tr>
<td><img src="doc/java.png" alt="plain JVM"/></td>
<td>

Add JVM parameter to launch agent(remove spaces):

<code>-javaagent: ${HOME}/.m2/repository/ io/mockk/mockk-agent/1.4.1/ mockk-agent-1.4.1.jar</code>

</td>
</tr>
</table>

## DSL examples

Simplest example:

```kotlin

    val car = mockk<Car>()

    every { car.drive(Direction.NORTH) } returns Outcome.OK

    car.drive(Direction.NORTH) // returns OK

    verify { car.drive(Direction.NORTH) }

```

### Partial argument matching

You can mix both regular arguments and matchers:<commit_msg>Version update in README<commit_after>![mockk](doc/logo-s-alt2.png) ![kotlin](doc/kotlin-logo.png)

[![Gitter](https://badges.gitter.im/mockk-io/Lobby.svg)](https://gitter.im/mockk-io/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge) [![Build Status](https://travis-ci.org/oleksiyp/mockk.svg?branch=master)](https://travis-ci.org/oleksiyp/mockk) [![Relase Version](https://img.shields.io/maven-central/v/io.mockk/mockk.svg?label=release)](http://search.maven.org/#search%7Cga%7C1%7Cmockk)  [![Change log](https://img.shields.io/badge/change%20log-%E2%96%A4-yellow.svg)](https://github.com/oleksiyp/mockk/releases) [![Back log](https://img.shields.io/badge/back%20log-%E2%96%A4-orange.svg)](/BACKLOG) [![codecov](https://codecov.io/gh/oleksiyp/mockk/branch/master/graph/badge.svg)](https://codecov.io/gh/oleksiyp/mockk) [![Documentation](https://img.shields.io/badge/documentation-%E2%86%93-yellowgreen.svg)](#nice-features)

Table of contents:

* auto-gen TOC:
{:toc}

## Nice features

 - removing finals (via runtime class modification)
 - pure kotlin mocking DSL
 - partial arguments matchers
 - chained calls / deep stubs
 - matcher expressions
 - mocking coroutines
 - capturing lambdas
 - bunch of matchers
 - few verification modes

## Examples

 - [kotlin-fullstack-sample](https://github.com/Kotlin/kotlin-fullstack-sample/pull/28/files#diff-eade18fbfd0abfb6338dbfa647b3215dR17) project covered with tests

## Installation

There is three installation steps.

#### 1. Add dependency

<table>
<thead><tr><th>Tool</th><th>Instruction</th></tr></thead>
<tr>
<td width="100"><img src="doc/gradle.png" alt="Gradle"/></td>
<td>
    <pre>testCompile "io.mockk:mockk:1.4.2"</pre>
    </td>
</tr>
<tr>
<td><img src="doc/maven.png" alt="Maven"/></td>
<td>
<pre>&lt;dependency&gt;
    &lt;groupId&gt;io.mockk&lt;/groupId&gt;
    &lt;artifactId&gt;mockk&lt;/artifactId&gt;
    &lt;version&gt;1.4.2&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;</pre>
    </td>
</tr>
</table>

#### 2. Add class modification via annotation

Known issue: JaCoCo works only with agent

<table>
<thead><tr><th>Tool</th><th>Instruction</th></tr></thead>
<tr>
<td width="100"><img src="doc/junit4.png" alt="JUnit4"/></td>
<td>
Use annotation for each test:

<code>@RunWith(MockKJUnit4Runner::class) </code>

Use @ChainedRunWith or @RunWith on superclass to override delegated runner.

If neither is specified the default JUnit runner is used.
</td>
</tr><tr>
<td><img src="doc/junit5.png" alt="JUnit5"/></td>
<td>

JUnit5 tests should work auto-magically.

Note: this implementation is totally a hack.

To disable class modification just create empty file resource 'io/mockk/junit/mockk-classloading-disabled.txt' on a classpath.

</td>
</tr><tr>
<td width="100"><img src="doc/testng.png" alt="TestNG"/></td>
<td>
Add to your test class:

<pre>
class MockKClassTransformer {
  @ObjectFactory
  fun getObjectFactory(): IObjectFactory {
    return MockKObjectFactory()
  }
}
</pre>
</td>
</tr>
</table>

#### 3. Add class modification via agent (optional)

<table>
<thead><tr><th>Tool</th><th>Instruction</th></tr></thead>
<tr>
<td width="100"><img src="doc/gradle.png" alt="Gradle"/></td>
<td>
Add <a href="https://github.com/Zoltu/application-agent-gradle-plugin">agent</a> gradle plugin.

Use following agent:

<code>agent "io.mockk:mockk-agent:1.4.2"</code>

</td>
</tr><tr>
<td><img src="doc/maven.png" alt="Maven"/></td>
<td>
Add <code>dependency:properties</code> plugin.

Configure maven surefire plugin:

<code>&lt;argLine&gt;-javaagent:${io.mockk:mockk-agent:jar}&lt;/argLine&gt;</code>

See example <a href="https://github.com/oleksiyp/mockk/blob/master/example/sum/pom.xml">here</a>
</td>
</tr><tr>
<td><img src="doc/java.png" alt="plain JVM"/></td>
<td>

Add JVM parameter to launch agent(remove spaces):

<code>-javaagent: ${HOME}/.m2/repository/ io/mockk/mockk-agent/1.4.2/ mockk-agent-1.4.2.jar</code>

</td>
</tr>
</table>

## DSL examples

Simplest example:

```kotlin

    val car = mockk<Car>()

    every { car.drive(Direction.NORTH) } returns Outcome.OK

    car.drive(Direction.NORTH) // returns OK

    verify { car.drive(Direction.NORTH) }

```

### Partial argument matching

You can mix both regular arguments and matchers:
{'fraction_non_alphanumeric': 0.1953955825639827, 'fraction_numerical': 0.017996961551945776, 'mean_word_length': 5.026760563380281, 'pattern_counts': {'":': 0, '<': 217, '<?xml version=': 0, '>': 217, 'https://': 28, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1707308', 'n_tokens_mistral': 3443, 'n_tokens_neox': 3242, 'n_words': 576}