Code Monkey home page Code Monkey logo

mockito-verify-subscriptions's Introduction

MIT License

Mockito verification for RxJava subscriptions

Mockito offers a powerful set of tools for JVM developers to create mock dependencies and verify particular method calls on them.

However, when your mocks return async RxJava types (e.g. Observable, Single, Completable) you often want to go a step further and verify that the reactive objects returned from your mocks were actually subscribed to.

This is a small extension to Mockito that allows you to do this.

More generally, this extension aims to allow you to verify asynchronous "invocations" on mocks in addition to regular synchronous ones, with minimal boilerplate code and impact on readability.

Basic Usage

Given a dependency you'd like to mock:

interface Repository {
    fun getItems(): Observable<String>
}

Create your mock passing a ReturnsTrackedRx1Types() or ReturnsTrackedRx2Types() as its default Answer:

val mockRepository = Mockito.mock(Repository::class.java, ReturnsTrackedRx1Types())

You can now verify subscriptions with wasSubscribedTo():

verify(mockRepository, wasSubscribedTo()).getItems()

I.e. this will cause your test to fail if the Observable returned by getItems() has not been subscribed to by the time it's run.

More Usage Examples

Verify that the mock's return value was never subscribed to with:

verify(mockRepository, neverSubscribedTo()).getItems()

Argument matchers work as expected:

verify(mockRepository, wasSubscribedTo()).getItems(limit = eq(10))

Calling reset on a mock will reset recorded subscriptions, just like regular invocations:

mockRepository.getItems().subscribe({}, {})

reset(mockRepository)

verify(mockRepository, wasSubscribedTo()).getItems()    // will fail!

For more usage examples, check out the Unit Tests.

RxJava 1 Support

wasSubscribedTo() will detect subscriptions on RxJava1 base types (Observable, Completable and Single) on mocks created with ReturnsTrackedRx1Types().

RxJava 2 Support

wasSubscribedTo() will detect subscriptions on RxJava2 base types (Flowable, Observable, Completable, Single and Maybe) on mocks created with ReturnsTrackedRx2Types().

Limitations (TODO)

This is currently missing support for:

  • In-order verifications
  • No informative error when attempting to use the verification without creating the mock correctly
  • Subscriptions can't be currently be tracked if you provide your own stubs for observable-returning methods

Download

Step 1.

Add the JitPack repository to your root build.gradle at the end of repositories:

allprojects {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2.

Add the dependency to your app's build.gradle

RxJava1:

dependencies {
    compile 'com.github.kiwiandroiddev.mockito-verify-subscriptions:rxjava1:v0.2-alpha'
}

RxJava2:

dependencies {
    compile 'com.github.kiwiandroiddev.mockito-verify-subscriptions:rxjava2:v0.2-alpha'
}

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.