Giter Club home page Giter Club logo

mockito-kt's Introduction

Mockito Kt

Apache License 2.0 Build Status Download

Purpose

This library provides some code-style cookies and tries to solve the problem with null values on matching

Example 1

val list = mock<MutableList<String>>()

list.add("String 1")
list.add("String 2")

list.verify {
    times(2).add(any())
}

Example 2

val list = MutableList::class.mock {
    on { size }.thenReturn(5)
    on { this[eq(3)] }.thenReturn("String 4")
}
assertEquals(5, list.size)
assertEquals("String 4", list[3])

Example 3

val list = ArrayList<String>().spy()
val set = spy<HashSet>() {
    ...
}

Example 4

// Mocking
val mockService = mock<ServiceClass>()
// Test
mockService.foo()
mockService.bar(SomeData("Test", 1))
// Verification
mockService.verifyOnce { match ->
    foo()
    bar(match.eq(SomeData("Test", 1)))
}

Matching and null values

Standard Mockito API does not always work fine due to the strict control null references in Kotlin. Mockito Kt tries to find default not null value, but in case when it impossible developer can register his own defaults manually. There is two options: local - for one mock object or globally - for all cases.

Local default value

mock<Foo> {
    defaults.register<Bar>(someBarValue)
    ...
}

Global default value

Defaults.Global.register<Bar>(someBarValue)

Using Maven

System requirements

Java 1.8+
Maven v3+
Kotlin 1.3

Repository settings

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>bintray-paslavsky-maven</id>
            <name>bintray</name>
            <url>https://dl.bintray.com/paslavsky/maven</url>
        </repository>
    </repositories>

Artifact

    <dependency>
        <groupId>net.paslavsky.kotlin</groupId>
        <artifactId>mockito-kt</artifactId>
        <version>2.0.1</version>
        <scope>test</scope>
    </dependency>

Using Gradle

System requirements

Java 1.8+
Gradle v3+
Kotlin 1.3

Repository settings

repositories {
    maven {
        url  "https://dl.bintray.com/paslavsky/maven" 
    }
}

Artifact

dependencies {
    testCompile 'net.paslavsky.kotlin:mockito-kt:2.0.1'
}

Migration from 1.0.0 to 2.0.0

In the second version, I changed the signature of the following methods:

fun <T : Any> mock(kClass: KClass<T>, setup: Mock<T>.() -> Unit = {}): T -> fun <T : Any> KClass<T>.mock(setup: Mock<T>.() -> Unit = {}): T
fun <T : Any> spy(obj: T, setup: Mock<T>.() -> Unit = {}): T -> fun <T : Any> T.spy(setup: Mock<T>.() -> Unit = {}): T
fun <T : Any> spy(classToSpy: KClass<T>, setup: Mock<T>.() -> Unit = {}): T -> fun <T : Any> KClass<T>.spy(setup: Mock<T>.() -> Unit = {}): T
fun <T : Any> verify(mock: T, verify: Verification<T>.() -> Unit) -> fun <T : Any> T.verify(verify: Verification<T>.() -> Unit)
fun <T: Any> verifyOnce(mock: T, checks: T.(match: MatchersKt) -> Unit) -> fun <T : Any> T.verifyOnce(checks: T.(match: MatchersKt) -> Unit)

It impossible to override old methods and mark as @Deprecated because from Java byte code perspective it's the same signature.

mockito-kt's People

Contributors

paslavsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mockito-kt's Issues

Cool to have default times(1) inside verify

Will be great to have the option to not use times(1).. just to avoid a lot of times(1) all over the place

Instead of:

       verify(mockedView) {
            times(1).initViews()
            times(1).setImage(user.photoUrl)
        }

have

       verify(mockedView) {
            initViews()
            setImage(user.photoUrl)
        }

Could not resolve mockito kt on maven repository

Hi, Im getting:

    > Could not resolve net.paslavsky.kotlin:mockito-kt:0.0.4-beta.
         > Could not get resource 'http://paslavsky.homeunix.net:8000/nexus/content/repositories/releases/net/paslavsky/kotlin/mockito-kt/0.0.4-beta/mockito-kt-0.0.4-beta.pom'.
            > Could not HEAD 'http://paslavsky.homeunix.net:8000/nexus/content/repositories/releases/net/paslavsky/kotlin/mockito-kt/0.0.4-beta/mockito-kt-0.0.4-beta.pom'. Received status code 503 from server: Ser

why?

Gradle Support for Android

Hey,

Could you make it available through Gradle?

I could use these improvements to test my Android app.

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.