A JMock wrapper for Kotlin

Related tags

App k-sera
Overview

K-Sera

A JMock wrapper for Kotlin.

KSeraExampleTests shows how to write a test.

class KSeraExampleTests {

    // Create a mockery using the usual JMock rule
    @Rule @JvmField val mockery = JUnitRuleMockery()

    // and a mock from the mockery
    val charSequence = mockery.mock<CharSequence>()

    @Test fun `expect a call to a mock via the mockery`() {
        mockery.expecting {
            oneOf(charSequence).length
        }
        // in this case JMock returns a default value
        assertEquals(0, charSequence.length)
    }

    @Test fun `you can specify a (type-checked) return value for a call`() {
        mockery.expecting {
            oneOf(charSequence).length.which will returnValue(42)
        }
        assertEquals(42, charSequence.length)

        // oneOf(charSequence).length.which will returnValue("banana") doesn't compile
    }

    @Test fun `simulate failures with throwException`() {
        mockery.expecting {
            oneOf(charSequence).length.which will throwException(RuntimeException("deliberate"))
        }
        assertFails {
            charSequence.length
        }
    }

    @Test fun `special operators work well`() {
        mockery.expecting {
            allowing(charSequence)[0].which will returnValue('*')
        }
        assertEquals('*', charSequence.get(0))
    }

    @Test fun `match parameters with Hamkrest`() {
        mockery.expecting {
            allowing(charSequence)[with(lessThan(0))].which will returnValue('-')
            allowing(charSequence)[with(anything)].which will returnValue('+')
        }
        assertEquals('-', charSequence.get(-1))
        assertEquals('+', charSequence.get(99))
    }

    @Test fun `which-will can take a block and access the call invocation`() {
        mockery.expecting {
            allowing(charSequence)[with(anything)].which will { invocation ->
                (invocation.getParameter(0) as Int).toChar()
            }
        }
        assertEquals(' ', charSequence[32])
        assertEquals('A', charSequence[65])
    }

    @Test fun `you can mock functions too`() {
        val f = mockery.mock<(Any?) -> String>()
        mockery.expecting {
            allowing(f).invoke(with(anything)).which will returnValue("banana")
        }
        assertEquals("banana", f("ignored"))
    }

    // Shall we play a game?

    val controlPanel = Panel(
        mockery.mock<Key>("key1"),
        mockery.mock<Key>("key2"),
        mockery.mock<Missile>())


    @Test fun `block syntax for expecting-during-verify`() =
        mockery {
            expecting {
                allowing(controlPanel.key1).isTurned.which will returnValue(true)
                allowing(controlPanel.key2).isTurned.which will returnValue(true)
            }
            during {
                controlPanel.pressButton()
            }
            verify {
                oneOf(controlPanel.missile).launch()
            }
        }

    @Test fun `given-that allows property references`() =
        mockery {
            given {
                that(controlPanel.key1, Key::isTurned).isProperty withValue (true)
                that(controlPanel.key2, Key::isTurned).isProperty withValue (false)
            }
            during {
                controlPanel.pressButton()
            }
            verify {
                never(controlPanel.missile).launch()
            }
        }

}

k-sera is available at Maven central.

You might also like...
MVVM + Kotlin + Jetpack Compose +Navigation Compose + Hilt + Retrofit + Unit Testing + Compose Testing + Coroutines + Kotlin Flow + Io mockK
MVVM + Kotlin + Jetpack Compose +Navigation Compose + Hilt + Retrofit + Unit Testing + Compose Testing + Coroutines + Kotlin Flow + Io mockK

MvvmKotlinJetpackCompose Why do we need an architecture even when you can make an app without it? let's say you created a project without any architec

A general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and efficient way

Timer Timer is a general purpose kotlin library that use kotlin coroutines, flows and channels to provide timer features with the most easy and effici

Solution to the 2021 Advent of code challenge in Kotlin. aoc-2021-in-kotlin

advent-of-code-2021 Welcome to the Advent of Code1 Kotlin project created by aniobistanley using the Advent of Code Kotlin Template delivered by JetBr

Android-basics-kotlin-tip-time-app - Tip Time app from Android Basics in Kotlin
Android-basics-kotlin-tip-time-app - Tip Time app from Android Basics in Kotlin

Tip Time Tip Time app from Android Basics in Kotlin at developers.google.com. It

Coinbase-pro-feed-kotlin - Kotlin Coinbase Pro Level 2 Order Book Feed
Coinbase-pro-feed-kotlin - Kotlin Coinbase Pro Level 2 Order Book Feed

Kotlin Coinbase Pro Level 2 Order Book Feed Quick start Depending on your OS run

Kotlin-basic-calculator - Basic calculator to understand syntax and the methods of Kotlin

KotlinBasicCalculator I always love to create a calculater for understand the sy

To Do List App is built in Kotlin using Material 3, Data Binding, Navigation Component Graphs, Room persistence library, Kotlin coroutines, LiveData, Dagger Hilt, and Notifications following MVVM Architecture.
To Do List App is built in Kotlin using Material 3, Data Binding, Navigation Component Graphs, Room persistence library, Kotlin coroutines, LiveData, Dagger Hilt, and Notifications following MVVM Architecture.

ToDoListApp ToDoList App demonstrates modern Android development with Hilt, Coroutines, LiveData, Jetpack (Room, ViewModel), and Material 3 Design bas

A showcase music app for Android entirely written using Kotlin language

Bandhook Kotlin This project is a small replica of the app I developed some time ago. Bandhook can still be found on Play Store At the moment it will

Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.
Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.

Palette Helper is a simple utility app made to generate color palettes of images using Google's fantastic Palette library. It's mostly a for-fun pet p

Owner
Duncan McGregor
Duncan McGregor
Android Studio project wrapper around the Elixir TodoApp Desktop app to run on Android including the Erlang runtime

TodoApp Android: An Android Sample App This Android Studio project wraps the Desktop Sample App to run on an Android phone. How to build & run Install

elixir-desktop 78 Dec 10, 2022
The Android wrapper of FlipClock.

FlipClock Android The Android wrapper for FlipClock. WHY? Fliqlo, the closed-source app for macOS has an iOS version, so I tried to make an Android ve

Alynx Zhou 37 Dec 29, 2022
Wrapper for Kustomer SDK v2.0 for react native

This is a wrapper for Kustomer v2 Sdk for react native. This implements the kust

Shivam Rawat 2 Dec 30, 2021
An easy-to-use wrapper for Lunar Client's game api.

An easy-to-use wrapper for Lunar Client's game api.

Patrick 5 Oct 25, 2022
Matomo wrapper for React-Native. Supports Android and iOS. Fixed issues for native platforms build that are present in the official package.

@mccsoft/react-native-matomo Matomo wrapper for React-Native. Supports Android and iOS. Fixed issues for native platforms build that are present in th

MCC Soft 4 Dec 29, 2022
A full-stack application showing the power šŸ’Ŗ of KOTLIN. Entire android app + backend Apis written in Kotlin šŸ”„

Gamebaaz ?? A full-stack application showing the power ?? of KOTLIN. Entire android app + backend Apis written in Kotlin ?? Android Backend Jetpack Co

Sarnava Konar 85 Nov 17, 2022
An simple image gallery app utilizing Unsplash API to showcase modern Android development architecture (MVVM + Kotlin + Retrofit2 + Hilt + Coroutines + Kotlin Flow + mockK + Espresso + Junit)

Imagine App An simple image gallery app utilizing Unsplash API. Built with ā¤ļøŽ by Wajahat Karim and contributors Features Popular photos with paginatio

Wajahat Karim 313 Jan 4, 2023
A collection of custom Android/Kotlin lint checks we use in our Android and Kotlin code bases at Slack.

slack-lints This repository contains a collection of custom Android/Kotlin lint checks we use in our Android and Kotlin code bases at Slack. This repo

Slack 119 Dec 20, 2022
Primeiro aplicativo desenvolvido na linguagem kotlin, durante o Hackathon no curso de Android Kotlin da Cel.Lep em parceria com EstaĆ§Ć£o Hack from Facebook

Tela Splash: Nome do aplicativo e uma imagem: Tela Main, principal tela do aplicativo: Um campo para o usuƔrio informar a medida; Um spinner com os ti

Maria Fortunato 0 Nov 3, 2021