Giter VIP home page Giter VIP logo

android-mockk's Introduction

Android-Mockk

Table of contents

1-Some useful resources related to Mockk

2-MockK

  • In Kotlin, all classes and methods are final. While this helps us write immutable code, it also causes some problems during testing.

  • Most JVM mock libraries have problems with mocking or stubbing final classes. Of course, we can add the “open” keyword to classes and methods that we want to mock. But changing classes only for mocking some code doesn’t feel like the best approach.

  • Here comes the MockK library, which offers support for Kotlin language features and constructs. MockK main intention is to be a convenient mocking library for everybody who develops in Kotlin.

Why is it better than a well known Mockito library for Kotlin?
Mockk supports some important language features within Kotlin.

1- Final by default(Classes and methods in kotlin) :

Concerning Java, Kotlin classes (and methods) are final by default. That means Mockito requires some extra things to make it to work, whereas Mockk can do this efficiently without any extra things.

2- Object mocking :

Kotlin objects mean Java statics. Mockito alone doesn’t support mocking of statics. There are the same other frameworks required with Mockito to write tests, but again Mockk provides this without any extra things.
Example :
mockObject(MyObject)
every { MyObject.someMethod() } returns "Something"

3- Extension functions :

Since extension functions map to Java statics, again, Mockito doesn’t support mocking them. With Mockk, you can mock them without any extra configuration.

4- Chained mocking :

With Mockk you can chain your mocking, with this we can mock statements quite easily like you can see in the example below. We are using every{} block to mock methods.
Example :
val mockedClass = mockk()
every { mockedClass.someMethod().someOtherMethod() } returns "Something"

5- Mocking static methods

Mocking static methods is not easy in mockito but using mockK java static methods can easily be mocked using mockStatic.
Example :
mockkStatic(TextUtils::class)
@Test
fun validateString() {
every { TextUtils.isEmpty(param } returns true
}

This is how you can mock static method isEmpty(param) of TextUtils class easily.

android-mockk's People

Contributors

mirtizakh avatar

Stargazers

 avatar

Watchers

 avatar

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.